无法使用ajax获取请求

时间:2022-10-12 20:44:01

Here is the error message: XMLHttpRequest cannot load (the url1). Origin http://localhost:8081 is not allowed by Access-Control-Allow-Origin.

这是错误消息:XMLHttpRequest无法加载(url1)。 Access-Control-Allow-Origin不允许使用origin http:// localhost:8081。

Here is the code:

这是代码:

$.ajax({
    url: (the url2),
    async : false,
    data: { fbId     : eh,
            fbSecret : meh,
            key      : bleh
          },
    datatype: "json",
    success: function(result){
                 console.log(result);
             }
});

I know the url is correct because when I click it it gives me the data I need, which is like {"names" : ["blah"]}

我知道网址是正确的,因为当我点击它时它会给我我需要的数据,就像{“names”:[“blah”]}

Do I need to give out any more details?

我需要提供更多细节吗?

I've tried various things like using jsonp/html instead of json, putting data directly into the url instead of separately as data, and using $.get instead of $.ajax, along with editing $.ajaxsetup....

我尝试了各种各样的东西,比如使用jsonp / html而不是json,将数据直接放入url而不是单独作为数据,并使用$ .get而不是$ .ajax,以及编辑$ .ajaxsetup ....

5 个解决方案

#1


1  

the error message says it all, you cannot make cross domain ajax requests (exception in case of jsonp but that also has to be supported by the server) due to same-origin-policy

错误信息说明了一切,你不能制作跨域的ajax请求(在jsonp的情况下例外,但也必须由服务器支持)由于同源策略

this may help you here http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-cross-domain-ajax-request-with-yql-and-jquery/

这可能对你有帮助http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-cross-domain-ajax-request-with-yql-and-jquery/

#2


0  

ports mismatch ;) the page that tries to establish the request has to be on the same port ;)

端口不匹配;)尝试建立请求的页面必须位于同一端口;)

#3


0  

I think you are trying to access a server different than the one you served the script from - the JavaScript code inside a page served from server A can access only server A using XmlHttpRequest.

我认为您正在尝试访问与您提供脚本的服务器不同的服务器 - 从服务器A提供的页面内的JavaScript代码只能使用XmlHttpRequest访问服务器A.

If this is the problem there is no easy solution - you have to use proxy or avoid XmlHttpRequest altogether.

如果这是问题,则没有简单的解决方案 - 您必须使用代理或完全避免使用XmlHttpRequest。

#4


0  

same-origin-policy is a browser security measure that restricts JavaScript code from talking with resources originating from other websites i.e resources loaded from any other domain and/or port. eg. JS running in a web page on http://google.com:80 cannot interact with data loaded from http://cbs.com or even http://cbs.com:8081

same-origin-policy是一种浏览器安全措施,它限制JavaScript代码与源自其他网站的资源进行通信,即从任何其他域和/或端口加载的资源。例如。在http://google.com:80上的网页中运行的JS无法与从http://cbs.com甚至http://cbs.com:8081加载的数据进行交互

Working around SOP a) Proxy in your server: you create a end point in your app that talks to the external url and returns the result

解决SOP a)服务器中的代理:您在应用中创建一个与外部URL对话并返回结果的终点

b) Load the JSON response into a <script> tag otherwise jsonp i.e json with padding

b)将JSON响应加载到

eg:

例如:

var url = "http://localhost:8081/?queryString=asdsa&callback=jsonCallback";

var script = document.createElement("script");
script.setAttribute("src", url);
script.setAttribute("type", "text/javascript");

window.jsonCallback = function(jsonObj) {
  // use JSON object here

  // cleanup
  document.body.removeChild(script);
  delete window[callback];
}

document.body.appendChild(script)

In your case since you are running it of a different port it is not valid and hence the error thrown by the browser..

在你的情况下,因为你运行的是另一个端口,它是无效的,因此浏览器抛出错误..

and there are some server side changes that go along with this.. read up on how to do it for your scripting language..

并且有一些服务器端更改与此一起...阅读如何为您的脚本语言执行此操作..

basically the response should be something like:

基本上响应应该是这样的:

jsonCallback({"Name": "Random", "Id" : 2432, "Rank": 453})

#5


0  

Use dataType: "JSONP" to overcome the limitation you encountered; check out the documentation to implement it correctly. You also have to provide a callback function for it.

使用dataType:“JSONP”克服您遇到的限制;查看文档以正确实现它。您还必须为它提供回调函数。

#1


1  

the error message says it all, you cannot make cross domain ajax requests (exception in case of jsonp but that also has to be supported by the server) due to same-origin-policy

错误信息说明了一切,你不能制作跨域的ajax请求(在jsonp的情况下例外,但也必须由服务器支持)由于同源策略

this may help you here http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-cross-domain-ajax-request-with-yql-and-jquery/

这可能对你有帮助http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-cross-domain-ajax-request-with-yql-and-jquery/

#2


0  

ports mismatch ;) the page that tries to establish the request has to be on the same port ;)

端口不匹配;)尝试建立请求的页面必须位于同一端口;)

#3


0  

I think you are trying to access a server different than the one you served the script from - the JavaScript code inside a page served from server A can access only server A using XmlHttpRequest.

我认为您正在尝试访问与您提供脚本的服务器不同的服务器 - 从服务器A提供的页面内的JavaScript代码只能使用XmlHttpRequest访问服务器A.

If this is the problem there is no easy solution - you have to use proxy or avoid XmlHttpRequest altogether.

如果这是问题,则没有简单的解决方案 - 您必须使用代理或完全避免使用XmlHttpRequest。

#4


0  

same-origin-policy is a browser security measure that restricts JavaScript code from talking with resources originating from other websites i.e resources loaded from any other domain and/or port. eg. JS running in a web page on http://google.com:80 cannot interact with data loaded from http://cbs.com or even http://cbs.com:8081

same-origin-policy是一种浏览器安全措施,它限制JavaScript代码与源自其他网站的资源进行通信,即从任何其他域和/或端口加载的资源。例如。在http://google.com:80上的网页中运行的JS无法与从http://cbs.com甚至http://cbs.com:8081加载的数据进行交互

Working around SOP a) Proxy in your server: you create a end point in your app that talks to the external url and returns the result

解决SOP a)服务器中的代理:您在应用中创建一个与外部URL对话并返回结果的终点

b) Load the JSON response into a <script> tag otherwise jsonp i.e json with padding

b)将JSON响应加载到

eg:

例如:

var url = "http://localhost:8081/?queryString=asdsa&callback=jsonCallback";

var script = document.createElement("script");
script.setAttribute("src", url);
script.setAttribute("type", "text/javascript");

window.jsonCallback = function(jsonObj) {
  // use JSON object here

  // cleanup
  document.body.removeChild(script);
  delete window[callback];
}

document.body.appendChild(script)

In your case since you are running it of a different port it is not valid and hence the error thrown by the browser..

在你的情况下,因为你运行的是另一个端口,它是无效的,因此浏览器抛出错误..

and there are some server side changes that go along with this.. read up on how to do it for your scripting language..

并且有一些服务器端更改与此一起...阅读如何为您的脚本语言执行此操作..

basically the response should be something like:

基本上响应应该是这样的:

jsonCallback({"Name": "Random", "Id" : 2432, "Rank": 453})

#5


0  

Use dataType: "JSONP" to overcome the limitation you encountered; check out the documentation to implement it correctly. You also have to provide a callback function for it.

使用dataType:“JSONP”克服您遇到的限制;查看文档以正确实现它。您还必须为它提供回调函数。