var response = $. ajax({ type: "GET", url: "http://www.google.de", async: false, success : function() { alert (this); } });
The responseText method is used for all formats that are not based on XML. It returns an exact representation of the response as a string. Plain text, (X)HTML, and JSON are all formats that use responseText.
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);
As Karim said, cross domain ajax doesn't work unless the server allows for it. In this case Google does not, BUT, there is a simple trick to get around this in many cases. Just have your local server pass the content retrieved through HTTP or HTTPS.
For example, if you were using PHP, you could:
Create the file web_root/ajax_responders/google.php with:
<?php
echo file_get_contents('http://www.google.de');
?>
And then alter your code to connect to that instead of to Google's domain directly in the javascript:
var response = $.ajax({ type: "GET",
url: "/ajax_responders/google.php",
async: false
}).responseText;
alert(response);
First you have to download a JQuery plugin to allow Cross-domain requests. Download it here: https://github.com/padolsey/jQuery-Plugins/downloads
Import the file called query.xdomainsajax.js into your project and include it with this code:
<script type="text/javascript" src="/path/to/the/file/jquery.xdomainajax.js"></script>
To get the html of an external web page in text form you can write this:
$.ajax({
url: "http://www.website.com",
type: 'GET',
success: function(res) {
var text = res.responseText;
// then you can manipulate your text as you wish
}
});
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With