Call WebService in JavaScript
As the straight left
Try calling a WebService in JavaScript. I think there are two things to watch out for.
1. Parameter transfer.
Generally, WebService calls require passing parameters. How to pass? As follows:
[WebService]
File: UserManage/the UserInfo. Asmx
Methods:
[WebMethod]
public string GetUserName(string accounts)
[JavaScript]
//WebService address and method name. Where GetUserName is the method to call
var URL = “UserManage/UserInfo.asmx/GetUserName”;
var Params = “accounts=leftfist”; // Parameters passed to the WebService
var xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
xmlhttp.Open(“POST”,URL, false); // Use the POST method
xmlhttp.SetRequestHeader(“Content-Type”,”application/x-www-form-urlencoded”);
xmlhttp.SetRequestHeader (“Content-Length”,Params.length);
xmlhttp.send(Params);
If (xmlhttp.Status == 200){//200 indicates success
var res = xmlhttp.responseXML; // Get the result returned by WebService
Return res.childNodes[1]. Text + “, “;
}
return xmlhttp.responseText;
2. Configuration file (web.config) problem.
Once the JavaScript script is written, there is no problem running it locally. But uploading to the server shows:
Request format is unrecognized for URL unexpectedly ending in ‘/GetUserName’
Estimate is “UserManage/the UserInfo. Asmx/GetUserName” deny, this written invoke the WebService server is looking for a long time just know, should be in the Web. Config plus:
<webServices>
<protocols>
<add name=”HttpGet”/>
<add name=”HttpPost”/>
</protocols>
</webServices>