views:

12162

answers:

5

neither:

var response = $.ajax({ type: "GET",   
      url: "http://www.google.de",   
      async: false,
      success : function()
      {
       alert (this);
      }
       });

nor:

var response2 = $.get("http://www.google.de", function(data){
          alert("Data Loaded: " + data);
          }
         );

give me an object, where a have access to teh resonseText?!?

A: 

in jquery ajax functions, the success callback signature is:

function (data, textStatus) {
  // data could be xmlDoc, jsonObj, html, text, etc...
  this; // the options for this ajax request
}

depending on the data type you've asked, using the 'dataType' parameter, you'll get the 'data' argument.

from the docs:

dataType (String) Default: Intelligent Guess (xml or html). The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently pass either responseXML or responseText to your success callback, based on the MIME type of the response.

The available types (and the result passed as the first argument to your success callback) are:

"xml": Returns a XML document that can be processed via jQuery.

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching unless option "cache" is used. Note: This will turn POSTs into GETs for remote-domain requests.

"json": Evaluates the response as JSON and returns a JavaScript Object.

"jsonp": Loads in a JSON block using JSONP. Will add an extra "?callback=?" to the end of your URL to specify the callback. (Added in jQuery 1.2)

"text": A plain text string.

see http://docs.jquery.com/Ajax/jQuery.ajax#options

Ken Egozi
see first other comment - response is empty -> no googleHtml text?! as response :-/
A: 

You simply must rewrite it like that:

var response = '';
$.ajax({ type: "GET",   
         url: "http://www.google.de",   
         async: false,
         success : function(text)
         {
             response = text;
         }
});

alert(response);
stoimen
mmh i tried it in ie8 and ff3.5.1 - in ff it will not work?!
it is empty -> no response - but why?
@all - cross domain ajax doesn't work unless the domain has jsonp enabled - which google does not
karim79
Even with jsonp compatible API, this does not work, because it seems that jQuery don't want to do sync ajax request to other domain... So the request is async. That's really, really boring... :-/
p4bl0
That is no better, the only difference is where you alert the user to the value of the variable. Also, the second option OP listed used $.get which is async by default. If he used your logic with the alert outside the event handler, it would always return '' whether the request was successful or not.
sillyMunky
+1  A: 

@all Karim is absolutely right. Yoc can't make ajax call to another domain. It is restricted to your own domain only.

Sanjay
A: 

The only way that I know that enables you to use ajax cross-domain is JSONP (http://ajaxian.com/archives/jsonp-json-with-padding).

And here's a post that posts some various techniques to achieve cross-domain ajax (http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide)

Xeross
+1  A: 

Actually, you can make cross domain requests with i.e. Firefox, se this for a overview: http://ajaxian.com/archives/cross-site-xmlhttprequest-in-firefox-3

Webkit and IE8 supports it as well in some fashion.

gustaf