简体   繁体   English

如何使用jquery将json数据发送到asmx(来自aspx)?

[英]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). 我在aspx 3.5中构建了联系表单,我使用jQuery将其发送到Web服务(asmx)。

The web service need to return success or error code. Web服务需要返回成功或错误代码。 The problem is that at the web method I get only single value and not array. 问题是在web方法中我只得到单个值而不是数组。 I'm kind of new in ajax and I tried a lot of solutions but without any results. 我是ajax的新手,我尝试了很多解决方案,但没有任何结果。 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: 在firebug中它正确发送:

{'name': 'jhon', 'company':'example', 'phone':'123', 'email':'jhon@jhon.com', 'questions':'hello'}

The asmx code is (please ignore the names, its example: asmx代码是(请忽略名称,其示例:

  [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. 当我调试时,我看到输入参数“name”只包含一个字符串 - 我不知道如何获取我发送给服务的完整json字符串并包含所有表单数据 - 我想将它转换为字符串数组或类似的东西,并处理它。 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: 问题不在客户端 - 它在服务器端 - 问题是我向Web服务发送了少量参数,但函数只有一个:

    [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? 你试过看request.form集合吗? since you are making post request and passing the params as a data to the request, it would be available in Request.Form. 由于您正在发布请求并将params作为数据传递给请求,因此它将在Request.Form中可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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