如何在javascript中实现异步服务调用?

如何在javascript中实现异步服务调用?,javascript,asynchronous,Javascript,Asynchronous,我想异步调用我的服务器 我的代码如下:- function GetSynchronousJSONResponse(url, postData) { var xmlhttp = null; if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest(); else if (window.ActiveXObject) { if (new ActiveXObject("Microsoft.XMLHTTP")) xmlhttp = new Active

我想异步调用我的服务器

我的代码如下:-

function GetSynchronousJSONResponse(url, postData) {
var xmlhttp = null;
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject) {
if (new ActiveXObject("Microsoft.XMLHTTP"))
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
else
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
xmlhttp.open("POST", url, false);
xmlhttp.send(postData);
var responseText = xmlhttp.responseText;
return responseText;
}
但服务调用给出了“错误请求”错误。
请帮助

函数GetSynchronousJSONResponse(URL,postData) {


您可以试试……。

您忘了在请求中添加内容类型。 请添加以下行,然后重试
xmlhttp.setRequestHeader(“Content-Type”,“application/json;charset=utf-8”);

显示postData值为什么不使用jQuery?这将在将来为您节省很多麻烦。您确实读过,不是吗?不要调用此函数
GetSynchronous…
!是的,在您答案的第一行。
        $.ajax({

           url : URL,

           type : "POST",

           data : JSON.stringify(postData),//if required

           contentType : 'application/json',

           success : function(data) {}
   })
}