0

I'm trying to pass parameters to my static web method (which is in an asp.net page). I'm trying to pass the "test1" param with a value of "myvalue". Any ideas on what I am doing wrong?

$.ajax({
    type: "POST",
    url: "WebForm1.aspx/WebMethod1",
    data: {"test1": "myvalue"},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert(msg.d);
    }
});
3
  • figured it out, my "data" section was wrong. it needs to be "{'test1':'myvalue'}" Commented Feb 4, 2009 at 8:34
  • Write it as an answer an get up-voted for answering your own question. :) Commented Feb 4, 2009 at 8:38
  • You should probably close the question? Commented Feb 4, 2009 at 9:52

3 Answers 3

1

my "data" section was wrong. it needs to be "{'test1':'myvalue'}"

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

Comments

0

What error are you getting?

I've used prototype before (similar to jQuery for ajax) and with that you don't quote the parameter names. So your data parameter should probably be:

data: {test1: "myvalue"}

Give that a shot.

You could also try setting up Fiddler and see the actual request being made.

1 Comment

Sorry, but this is incorrect. What you show is a JavaScript object. That needs to be converted to a JSON string, like the question-asker how shown: date: {"test1":"myvalue"}
0

The way you have it set up, any error that may occur in the ajax call will get silently swallowed. I'd suggest adding an error callback like this:

$.ajax({
    type: "POST",
    url: "WebForm1.aspx/WebMethod1",
    data: {"test1": "myvalue"},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert(msg.d);
    },
    error: function(response) {
        $('body',document).html(response.responseText);
    }
});

Also, if you are using Visual Studio, you could run your ASP.NET app in debug mode to catch any server-side error. You can also put a breakpoint somewhere in the server-side code to make sure it's getting hit at all, and to inspect the Request.Form collection.

Hope this helps.

Comments

Your Answer

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