无法通过XMLHttpRequest发送参数字符串
问题描述:
您好,我试图从Web服务获取响应,但是当我通过XMLHttpRequest.send(params)方法发送请求参数时,请求参数不会被发送。无法通过XMLHttpRequest发送参数字符串
下面是我的代码:
<script type="application/javascript">
window.onload = function myFunc()
{
var httpRes;
if (window.XMLHttpRequest)
{
httpRes=new XMLHttpRequest();
}
else
{
httpRes=new ActiveXObject("Microsoft.XMLHTTP");
}
httpRes.open("POST", "http://192.168.11.59:3333/Reports/GenerateMobReportJsonData", true);
var params = {'FromDate':'02/19/2013 17:30','ReportId':'1','LocationId':'1','ToDate':'02/19/2013 19:00','TeamId':'1'}
httpRes.setRequestHeader('content-type', 'application/json');
var jsonReq = JSON.stringify(params);
//alert(jsonReq)
httpRes.send(jsonReq);
}
</script>
任何形式的帮助是非常赞赏...
答
当您使用“应用程序/ Json的”作为您的通信协议和你的jQuery托管服务器和网络业务服务器是不同的,可以有跨域请求
这里的一个问题,你应该使用JSONP作为通信协议。
下面是一个例子 - 两个不同的服务器之间
http://www.jquery4u.com/json/jsonp-examples/
请求应JSONP协议去。
也是一个类似的问题在这里 -
jQuery Ajax fails even with the HTTP 200 status
希望这有助于你。
是你的jquery托管服务器和Web服务托管服务器上相同的IP? – 2013-03-12 11:54:52
@PiyasDe:no ... – Rocker 2013-03-12 11:55:56
尝试httpRes.open(“POST”,“http://192.168.11.59:3333/Reports/GenerateMobReportJsonData”,false); – MarmiK 2013-03-12 11:56:38