简体   繁体   中英

how to send json data to asmx (from aspx) using jquery?

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':'jhon@jhon.com', '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 don't 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?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM