Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-Domain Requests with jQuery

For a project I need to get the source code of web page of different other domains. I have tried following code:

$('#container').load('http://google.com');

$.ajax({
    url: 'http://news.bbc.co.uk',
    type: 'GET',
    success: function(res) {
        var headline = $(res.responseText).find('a.tsh').text();
        alert(headline);
    }
});

Still I am not getting any results but just a blank alert box.

like image 718
Saurabh Saxena Avatar asked Oct 03 '11 17:10

Saurabh Saxena


People also ask

Can you do cross domain AJAX?

You can allow Cross Domain Ajax calls to an application by just registering a new filter and then configure it to Allow-Origin : {your domain's} or you can use a wild card “*” to allow the calls from all domains.

How do I send a cross domain POST request via JavaScript?

This will become clear as you continue to read... Setup your cross domain POST from JS (jQuery example): $. ajax({ type: 'POST', url: 'https://to.com/postHere.php', crossDomain: true, data: '{"some":"json"}', dataType: 'json', success: function(responseData, textStatus, jqXHR) { var value = responseData.

How do I create a cross-origin AJAX request?

Clicking the "Fetch HTML5 Rocks" button generates an Ajax call (via jQuery's $. ajax method) to http://www.html5rocks.com/en/tutorials/file/xhr2/. We set local JavaScript variable article to the contents of the first article found in the returned response and display the contents on our page.

Which are the methods used for cross domain AJAX calls?

For a successful cross-domain communication, we need to use dataType “jsonp” in jquery ajax call. JSONP or “JSON with padding” is a complement to the base JSON data format which provides a method to request data from a server in a different domain, something prohibited by typical web browsers.


1 Answers

By default all browsers restrict cross-domain requests, you can get around this by using YQL as a proxy. See a guide here: http://ajaxian.com/archives/using-yql-as-a-proxy-for-cross-domain-ajax

like image 62
Porco Avatar answered Oct 21 '22 11:10

Porco