calling asmx web service from jQuery
I am not able to call web service(asmx) from jQuery function.
It is saying "access denied" error while calling web service. It is working in the dev and local machine but I am getting the same error.
Here is my ajax call
$.ajax({
type: "POST",
url: "http://server.com/calculator.asmx/calculus",
data: "{ 'userID': '" + $("#usrid").val() + "','password': '" + $("#password").val() + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: Success,
error: Error
});
My web service is
[WebService(Namespace = "http://www.company.com/webservices/calculus")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System开发者_如何转开发.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class calculator : System.Web.Services.WebService
{
[WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet=false, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public bool calculus(string userName, string password)
{// my code}
The error is in http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js function and the "Access denied" error at e.username?x.open(n,e.url,e.async,e.username,e.password):x.open(n,e.url,e.async);
I have included [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] with the class as shown in http://forums.asp.net/p/1570168/3935094.aspx and not able to fix the prob. Can any one please help me regarding this.
Thank you
AJAX calls are bound to the same origin policy meaning that you cannot invoke a web service which is situated on a different domain. The browser will simply drop the request. One possible solution would be to write a server side script on the same domain which will serve as bridge to the actual web service and then call this script.
JSONP is a possible way to workaround the "same origin policy" (aka cross-site-scripting or XSS) limitations. It comes with its own set of challenges (for example, it works only with GET-mode requests), so it's certainly not a panacea. But it's probably worth your time to give it a look. There's a decent number of stackoverflow postings about it, which should help you get started.
精彩评论