简体   繁体   English

无法使用ajax发出获取请求

[英]Can't make get request using ajax

Here is the error message: XMLHttpRequest cannot load (the url1). 这是错误消息:XMLHttpRequest无法加载(url1)。 Origin http://localhost:8081 is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-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 ....

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/

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

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。

same-origin-policy is a browser security measure that restricts JavaScript code from talking with resources originating from other websites ie resources loaded from any other domain and/or port. same-origin-policy是一种浏览器安全措施,它限制JavaScript代码与来自其他网站的资源(即从任何其他域和/或端口加载的资源)进行交谈。 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 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 ie json with padding b)将JSON响应加载到<script>标签中,否则将jsonp即带填充的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})

Use dataType: "JSONP" to overcome the limitation you encountered; 使用dataType: "JSONP"克服您遇到的限制; check out the documentation to implement it correctly. 查看文档以正确实施。 You also have to provide a callback function for it. 您还必须为其提供回调函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM