jquery ajax永远不会工作

时间:2014-03-04 15:07:28

标签: jquery ajax

我有这个代码,它总是以错误块结束。我花了四个小时搜索,但没有任何帮助。我想从URL加载HTML数据

$.ajax({
url: 'http://google.com',
//data: {action : 'login', formData : $('#check-user').serialize()},
dataType: 'html',
type: 'get',                   
async: 'false',
//contentType: 'application/json; charset=utf-8',
//dataType: 'json',
beforeSend: function(xhr) {
    //xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
    // This callback function will trigger before data is sent
    //$.mobile.showPageLoadingMsg(true); // This will show ajax spinner
},
complete: function() {
    // This callback function will trigger on data sent/received complete
   // $.mobile.hidePageLoadingMsg(); // This will hide ajax spinner
},
success: function (result) {
    alert(result);
    if(result.status) {
        //$.mobile.changePage("#second"); 
        alert("Change Page");
    } else {
        alert('Logon unsuccessful!'); 
    }
},
error: function (xhr, status, error) {
    // This callback function will trigger on unsuccessful action             
    //var err =  eval(xhr.responseText);
    //alert(xhr.Message);
    alert('Network error has occurred please try again! ' );
}
});                   

2 个答案:

答案 0 :(得分:1)

看看:

http://en.wikipedia.org/wiki/Same-origin_policy

可以从外部网站加载脚本和图像,但不能加载html。

你需要抓住服务器端

答案 1 :(得分:0)

除非服务器允许,否则跨域ajax不起作用。在这种情况下谷歌没有。 然而,有一些插件和技巧允许这样做, 另一种选择是建立请求服务器端。

编辑:我找到了这个,这可能会有所帮助: http://ajaxian.com/archives/cross-site-xmlhttprequest-in-firefox-3

它适用于Firefox,但Webkit和IE8也以某种方式支持它。

相关问题