Simple cross domain html request with jquery? Tryi

2019-05-30 11:29发布

I'm trying to perform a simple html request using jquery with the code below.

$.get('http://externalsite.com/status.html', function(data) {

if (data == 1) {
//do something
}

else {
//do something else
}

});

I'm using the cross domain request plugin found at http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/ and I added the .js script to my html file, but what's next? The example uses a #container and a load from google, but I'm lost as to what I do for an html request. It says it works for any .get request, I just don't know how to do it. Thanks for your help.

2条回答
虎瘦雄心在
2楼-- · 2019-05-30 11:39

You can do whatever you want. Read the jQuery docs for .load() and .get() :

This example puts the contents of http://google.com in the HTML element with id="container":

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

This does the same thing but with your example site:

$('#container').load('http://externalsite.com/status.html');

This also does the same thing as above:

$.get('http://externalsite.com/status.html', function(data) {    
    $('#container').html(data);
});
查看更多
Emotional °昔
3楼-- · 2019-05-30 11:53

i am not sure what your problem exactly is, can you for example wrap this code into the function and simple invoke it ? :) like

function makeAjaxCall(url){
 $.get(url, function (...)
}

makeAjaxCall('http://google.com');
查看更多
登录 后发表回答