1

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

4
  • 2
    Is the web service on another domain? Commented Oct 1, 2010 at 13:52
  • Agree with @Nick, sounds (and looks) like a cross domain issue Commented Oct 1, 2010 at 14:01
  • I'm not sure, but with dataType 'json' shouldn't your data be an object rather than a string? Commented Oct 1, 2010 at 14:04
  • Yeah. the web service in another domain. It actually working in the local server though but not from any client web browser. Commented Oct 1, 2010 at 14:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Darin. i figured out the prob. This is cross domain issue. But now the problem is calling web service working fine from IE but neither from mozilla or chrome.. I can see web methods and all when I give the direct url but it is not calling from jQuery again
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.