calling asmx web service from jQuery

陌路散爱 提交于 2019-12-24 15:43:46

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3839787/calling-asmx-web-service-from-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!