Simple cross domain html request with jquery? Trying to use plug-in

给你一囗甜甜゛ 提交于 2019-12-08 09:01:30

问题


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.


回答1:


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);
});



回答2:


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');


来源:https://stackoverflow.com/questions/6935477/simple-cross-domain-html-request-with-jquery-trying-to-use-plug-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!