views:

182

answers:

2

i built contact form in aspx 3.5 and i'm using jquery to send it to web service (asmx).
the web service need to return success or error code. the problem is that at the web method i get only single value and not array. I'm kind of new in ajax and i tried a lot of solutions but without any results.. please if you can only explain me the principle of what to do it also be good..

this is the client side:

$(document).ready(function() 
{
    $("#submit").click(function(event)
    {
        $.ajax
        ({
            type: "POST",
            url: "RVContactFormMailer.asmx/HelloToYou",                
            data: "{'name': '" + $('#name').val() + "', 'company':'" + $('#company').val() + "', 'phone':'" + $('#phone').val() + "', 'email':'" + $('#email').val() + "', 'questions':'" + $('#questions').val() + "'}" ,                 
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
            AjaxSucceeded(msg);
         }, error: AjaxFailed
        });
   });

in firebug its sends correctly:

{'name': 'jhon', 'company':'example', 'phone':'123', 'email':'[email protected]', 'questions':'hello'}

the asmx code is (please ignore the names, its example..:

  [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService] // To allow this Web Service to be called from script, using ASP.NET AJAX or JQuery.
    [ToolboxItem(false)]
    public class RVContactFormMailer : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]  
        public string HelloToYou(string name)
        {
            return "Hello " + name;
        }
    }

when i debug i see that the input parameter "name" contains only one string - i dont know how to get the full json string that i send to the service and contains all the form data - i want to desirialize it to string array or something like, and process it. how can i do that? Thanks, Eranio.

A: 

Hi, did you tried looking at request.form collection? since you are making post request and passing the params as a data to the request, it would be available in Request.Form.

lakhlaniprashant.blogspot.com
when i look at the this.Context.Request.Form, its empty.how can i get the json string that i sent to the web service?
eran
ok, if you are getting the name correctly, then I will suggest to do this: {param:{'name': 'jhon', 'company':'example', 'phone':'123', 'email':'[email protected]', 'questions':'hello'}}
lakhlaniprashant.blogspot.com
A: 

the problem was not in the client side - it was in the server side - the problem is that i send few parameters to the web service but the function get only one:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloToYou(string name)

while the correct one should be:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloToYou(string name, string company, string phone, string email, string questions)

anyway, Thanks for the help!

eran
The OTHER option would have been to create a complex object on the client side and deserialize it on the server as a named object with that complexity thus only having a single server side object passed.
Mark Schultheiss
Mark can you please give more info/example or link to some article? thanks
eran
i use the info in: http://stackoverflow.com/questions/2785441/asp-net-json-web-service-post-form-dataand send complex object as json.thanks
eran