We help IT Professionals succeed at work.
Get Started

jQuery ajax get causing page to reload

libby9284 asked
Last Modified: 2012-08-15

I'm trying to integrate the google custom search API into my code with the following javascript / jQuery function.  With the redacted items filled in this sort of works, with one major problem - the whole page appears to refresh and I lose my search results (unless I'm working through the code stepwise via firebug).

Can anyone suggest a way to keep this refresh from happening and / or maintain my results?  Its kind of useless if I can't *show* the results generated.

I am 100% certain that the myQuery variable does get formed correctly, so that is not the problem (other testing verified it)

Thanks in advance!

Some other notes:

this code is part of a mono C# .net web application that employs the use of ajax control toolkit update panels

		function handleGoogleSearch(searchQuery)
			var cx = 'cx=redacted_search_engine_id';
			var exactTerms = "q=" + searchQuery;
			var googlehost = "googlehost=google.com";
			var lr = "lr=lang_en";
			var safety = "safe=high";
			var access_token="redacted_google_api_key";
			var format = "alt=json";
			var myQuery = "https://www.googleapis.com/customsearch/v1?key="+access_token+"&"+cx+"&"+exactTerms+"&"+lr+"&"+safety+"&"+format;//+"&callback=parseTheSearch";
				type: "GET",
				contentType: "application/json; charset=utf-8",
				url: myQuery,
				dataType: "json",
				success: function(response)
							var theData = "";
							for (var i = 0; i<response.items.length; i++)
								var theItem = response.items[i];
								theData += "<a href='"+theItem.link+"'>"+ theItem.title + "</a><br/>"+theItem.displayLink+"<br/>"+theItem.snippet+"<br/><br/>";
							document.getElementById("searchContentPanel").innerHTML = theData;
				error: function()
							alert("I'm sorry but I am unable to contact google at this time");

Open in new window

Watch Question
Distinguished Expert 2020
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE