AJAX POST错误:NETWORK_ERR:XMLHtt prequest异常101异常、错误、POST、AJAX

2023-09-10 16:45:08 作者:对不起 ブ我还爱你

我有以下阿贾克斯后:

$.ajax( {
    type: "POST",
    url: "http://192.168.7.9/api",
    dataType: 'json',
    data: { username: "john.doe", password: "123456", method: "search_samples" },
    success: function ( data ) {
       // Never get here
    },
    error: function ( XMLHttpRequest, textStatus, errorThrown ) {
        // Always here: if async true, errorThrown has no message
        // otherwise I se the NETWORK_ERR message
    }
} );

这是返回与此错误:NETWORK_ERR:XMLHtt prequest异常101

It is returning with this error: NETWORK_ERR: XMLHttpRequest Exception 101.

我读了一堆关于此错误的SO帖子,大多数建议我异步设置为true。这并删除错误MESSAGE-但它仍然是一个错误,我从来没有得到有效的数据。这似乎只是除去这是没有帮助的错误消息。

I have read a bunch of SO posts on this error, most suggest that I set async to true. This DOES remove the error message- but it is still an error, and I never get valid data. It just seems to remove the error message which is not helpful.

在小提琴手,同样的开发计算机上这工作perfectly-这是一个镀铬的问题?一个起源的问题?有什么问题我的语法?

In fiddler, on the same dev machine this works perfectly- is this a chrome issue? An origin issue? Is something wrong with my syntax?

推荐答案

确定,它看起来像你正在运行到与的同源策略。你正在做的方式,你不能从一个不同的服务器承载你的应用访问AJAX的数据。

OK, it looks like you are running into issues with the same origin policy. The way you are doing it, you can't access AJAX data from a different server than the one that is hosting your application.

要做到这一点,你要么必须移动 http://192.168.7.9/api 的功能到您的服务器或使用JSONP来传输数据。 这里是如何做到这一点的例子。

To do so, you would either have to move the http://192.168.7.9/api functionality onto your server or use JSONP to transfer the data. Here is an example of how to do so.