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

发布于 2024-09-06 01:35:15 字数 1797 浏览 5 评论 0原文

我在 aspx 3.5 中构建了联系表单,并使用 jQuery 将其发送到 Web 服务 (asmx)。

Web服务需要返回成功或错误代码。问题是在网络方法中我只得到单个值而不是数组。我是 ajax 新手,尝试了很多解决方案,但没有任何结果。如果您只能向我解释一下做什么的原理,那也很好。

这是客户端:

$(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
        });
   });

在 firebug 中,它正确发送:

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

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;
        }
    }

当我调试时,我看到输入参数“name”仅包含一个字符串 - 我不知道如何获取我发送到服务并包含所有表单数据的完整 json 字符串 - 我想将其反序列化为字符串数组或类似的东西,并处理它。 我怎样才能做到这一点?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨的味道风的声音 2024-09-13 01:35:15

问题不在客户端 - 它在服务器端 - 问题是我向网络服务发送了几个参数,但该函数只得到一个:

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

而正确的应该是:

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

无论如何,感谢帮助!

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!

始终不够 2024-09-13 01:35:15

您是否尝试过查看 request.form 集合?由于您正在发出发布请求并将参数作为数据传递给请求,因此它将在 Request.Form 中可用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文