views:

235

answers:

1

Hi, I'm not very experienced in C#/.NET [WebMethod] (note: I am on Mono) and I wonder how could I use the JQuery.form plutin. I have a Service.asmx and Service.asmx.cs with methods I call with standard jQuery AJAX call.

In the form "action" attribute I put link to the Service (/blah/blah/Service.asmx/myMethod). Which firm and/or attributes must have the 'myMethod' to accept the POST message?

Thank you

A: 

It really doesn't matter that you are using [WebMethod] in this case. You just need to know how to format your HTML form. Here's what it should look like:

<form id="myForm" action="/blah/blah/Service.asmx/myMethod/" method="post">
....
</form>

To setup the jquery, you just have to specify what you're returning. Here's the general form, which you can customize to your needs.

$("#myForm").ajaxForm({
    dataType: 'json',
    success: function (responseText, statusText) {
        ...
    },
    beforeSubmit: function (formData, jqForm, options) {
        ....
    }
});

The options for dataType is json, xml, script or none. In the success function, that is where you handle the returned ajax data.

Hope that helps.

zowens
Thanks for the answer, but in C# code how could I access the JSON object coming from the POST?[code]public void myMethod(???) { // How access the data here?}[/code]Thanks for the patience, really n00b here.
fullOfRant
You don't handle JSON in C#, you handle JSON with JavaScript. The datatype is the type of the returned value of the request. You would handle the request values in the web method like you would any web request, with using Request["key"] with the posted form values.
zowens