如何发送一个不同的端口与jQuery的AJAX请求? [英] How do I send an AJAX request on a different port with jQuery?

查看:1675
本文介绍了如何发送一个不同的端口与jQuery的AJAX请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送一个Ajax请求,例如8080端口,其中一个守护进程运行在那里。

I need to send an AJAX request to, for example, port 8080 where a daemon is running there.

推荐答案

您不能 POST 信息的跨域名,子域名或端口号。但是,您可以使用JSONP,如果你有机会到这两个守护进程的的请求的网站。如果数据需要被退回,那么守护需要支持回调查询参数,并返回其格式正确。

You cannot POST information cross domain, subdomain, or port number. You can however use JSONP if you have access to both the daemon and the requesting site. If data needs to be returned, then the daemon needs to support a callback query parameter and return it properly formatted.

将信息传递到后台程序:

Pass the information to the daemon:

$.getJSON('http://domain.com:8080/url/here?callback=?', {
  key: 'value',
  otherKey: 'otherValue'
}, function(data){
     // Handles the callback when the data returns
});

现在只需要确保您的守护程序处理的回调参数。例如,如果回调= myCallBack函数从守护程序(写入页面的唯一的事情)的回归应该是这样的:

Now just make sure your daemon handles the callback parameter. For instance, if callback=mycallback the return from the daemon (the only thing written to the page) should look like this:

有关的键/值对:

mycallback( {'returnkey':'returnvalue', 'other':'data' });

对于数组:

mycallback( [1,2,3] );

如果您还没有到位JSONP或类似机制,则无法使用jQuery沟通跨域。

If you do not have a JSONP or similar mechanism in place, you cannot communicate cross domain using jQuery.

这篇关于如何发送一个不同的端口与jQuery的AJAX请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆