简体   繁体   English

jQuery:$ .getJSON具有不同的URL端口

[英]Jquery: $.getJSON with different url port

I am trying to use $.getJSON with a local app calling another local app on a different port. 我正在尝试将$.getJSON与在另一个端口上调用另一个本地应用程序的本地应用程序一起使用。 For example my app is running on localhost:3000 , but I want to make a $.getJSON call to another app running on localhost:3001 , in firebug it returns red with a 200 response, but with no data in the response. 例如,我的应用程序在localhost:3000上运行,但是我想对运行在localhost:3001上的另一个应用程序进行$.getJSON调用,在萤火虫中它返回红色,响应为200,但响应中没有数据。 Is there a way to do this? 有没有办法做到这一点? I tried this.... 我尝试了这个。

 $.getJSON('http://localhost:3001/dashboard/widgets/marketing_efficiency_gauge.json',
 {   key: 'value' }, function(data){

 alert(data)

  });

Edit: for clarity there are two rails apps involved one on localhost:3000 another on localhost:3001 编辑:为清楚起见,有两个Rails应用程序涉及一个在localhost:3000上,另一个在localhost:3001上

Second edit: here is the json response for localhost:3001 when I hit it with a browser (say firefox) https://gist.github.com/willfults/7665299 第二次编辑:这是当我用浏览器(例如firefox)点击时对localhost:3001的json响应https://gist.github.com/willfults/7665299

The Same Origin Policy prevents JavaScript scripts from making HTTP requests to different domains. 相同起源策略可防止J​​avaScript脚本向不同域发出HTTP请求。 For the purposes of SOP, a URL with the same hostname but different ports (as is the case here) is still considered to be a different domain, and hence requests are not permitted. 出于SOP的目的,具有相同主机名但不同端口(如此处的情况)的URL仍被视为不同的域,因此不允许请求。

What typically happens in such cases is that the browser actually does make the request over the network, but drops the response and sends an error result to the JavaScript. 在这种情况下,通常发生的情况是浏览器实际上确实通过网络发出了请求,但是丢弃了响应并将错误结果发送给JavaScript。

To fix this, you'll need to implement Cross-Origin Resource Sharing on the localhost:3001 service. 要解决此问题,您需要在localhost:3001服务上实现跨域资源共享 In a nutshell, this entails adding a Access-Control-Allow-Origin header to responses listing the domains which are permitted to make cross-domain requests to the service. 简而言之,这需要在响应列表中添加Access-Control-Allow-Origin标头,以列出允许向服务发出跨域请求的域。 That is, in this case adding a Access-Control-Allow-Origin: localhost:3000 header to the response from the localhost:3001 service should allow things to work as you expect. 也就是说,在这种情况下,将Access-Control-Allow-Origin: localhost:3000标头添加到来自localhost:3001服务的响应中,应该可以使事情按预期工作。

Incidentally, this is why the browser makes the request but drops the result: it needs to request the headers from the server in order to determine whether the JavaScript is allowed to make the request or not (ie it needs to check if there's a Access-Control-Allow-Origin header in the response). 顺便说一句,这就是为什么浏览器发出请求但丢弃结果的原因:它需要从服务器请求标头,以确定是否允许JavaScript发出请求(即,它需要检查是否存在Access-Control-Allow-Origin响应中的Access-Control-Allow-Origin标头)。 Why a HEAD request isn't sufficient, I don't know. 我不知道为什么HEAD请求不够用。

The other alternative is to use JSONP . 另一种选择是使用JSONP This is potentially simpler to implement on the server side, but has the disadvantages of only working for GET requests, and requiring slightly trickier coding on the client side. 这可能在服务器端更容易实现,但缺点是只能处理GET请求,并且需要在客户端进行更复杂的编码。

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

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