0

I don't get why $.ajax() function cannot reach my [WebMethod].

Here is the jQuery below:

$('.BasketUpdaterSubmit').click(function() {
$.ajax({
    url: '/Ajax/AjaxCalls.aspx/UpdateAsyncBasket',
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: "{'name' : 'Ivan'}",
    success: function(data) { alert(data); },
    error: function(xhr) { alert("Damn!"); }
});

Here is the C# code:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string UpdateAsyncBasket(string name)
    {
        // random stuff
        return "Received : \t " + name;
    }

When I place a breakpoint on the return statement I never seem to get there. What am I doing wrong?

13
  • 1
    can you statically access /Ajax/AjaxCalls.aspx/UpdateAsyncBasket (In a web browser ?) Commented May 15, 2013 at 14:58
  • your json also doesn't seem to be valid json, does ASP care that it isn't valid json? (single quotes should be double) Commented May 15, 2013 at 15:07
  • Does your click() event actually execute? Commented May 15, 2013 at 15:07
  • 1
    try UseHttpGet = false before ResponseFormat.Json Commented May 15, 2013 at 15:10
  • 1
    Have you tried using Firebug and examinig the actual response - you will get a message saying more or less what the actual problem with the request is...or at the very least examine the xhr object and see what the responseText is. Commented May 15, 2013 at 18:49

4 Answers 4

1

Based on the experience I've had with this stuff, I THINK the JS has to be put inside inside .NET's page load javascript function to access C# web methods.

function pageLoad(sender, eventArgs) { }

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

Comments

1

Try a GET instead of a POST. I have a few web methods that work fine using similar javascript, but have decorated the method with this instead of just [WebMethod]:

[WebMethod, ScriptMethod(UseHttpGet = true)]

And then make sure your ajax call specifies GET:

$('.BasketUpdaterSubmit').click(function() {
$.ajax({
    url: '/Ajax/AjaxCalls.aspx/UpdateAsyncBasket',
    type: 'GET',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: "{'name' : 'Ivan'}",
    success: function(data) { alert(data); },
    error: function(xhr) { alert("Damn!"); }
});

1 Comment

Will try this immediately once i have access to my code again.
1

Try this code,

$(document).on("click",".BasketUpdaterSubmit",function(){    
    $.ajax({
        url: '/Ajax/AjaxCalls.aspx/UpdateAsyncBasket',
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'name' : 'Ivan'}",
        success: function(data) { alert(data); },
        error: function(xhr) { alert("Damn!"); }
    });    
});

And the web.config you have to add following section inside the system.web section

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

Above code should work(tested in my machine). My best suggestion for you is create a web service for this. so you can omit the page life cycle. You can refer following sample to learn how to do it properly http://tutorials.cmsnsoftware.com/2011/01/how-to-call-csharp-function-in-ajax.html

Comments

0

We figured out what's going on. It seems that there is a default ajax setup function which was interfering with my jquery function and didn't proceed when testing. Most probable reason is improper parameter handling. Sorry for the late response. Thank you all.

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.