调用webmethod无法通过javascript工作。错误代码400和500

时间:2015-03-23 06:55:32

标签: javascript asp.net ajax xmlhttprequest webmethod

我试图像xmlhttp对象那样调用webmethod

 var v2 = JSON.stringify({ province: 'Ontario' });
    xmlhttp.open("POST", "http://localhost:49771/rentalone/lp.aspx/getCities?" + v2, true);

问题是当我使用“?”时像这样

  /getCities?" + v2

它为我提供了错误代码500“缺少参数”

的值

但是当我使用“/”

   /getCities/" + v2

它给我错误代码http 400.床位要求

如何调用具有输入参数的webmethod?

1 个答案:

答案 0 :(得分:0)

也许改变它以便v2='province=ontario';

好的......那不行,所以也许尝试这样的事情(从评论中的链接修改):     var v2 = JSON.stringify({province:'Ontario'});     var http = new XMLHttpRequest();

http.open("POST", "http://localhost:49771/rentalone/lp.aspx/getCities", true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Content-length", v2.length);

http.onreadystatechange = function() {//Call a function when the state changes.
   if(http.readyState == 4 && http.status == 200) {
      alert(http.responseText);
   }
}
http.send(v2);