从jQuery的传递一个用户定义的对象ASP.NET的WebMethod,使用JSON [英] Pass a user defined object to ASP.NET Webmethod from jQuery, using JSON

查看:143
本文介绍了从jQuery的传递一个用户定义的对象ASP.NET的WebMethod,使用JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些简单的JSON传递到jQuery的从一个ASP.NET 4.5的WebMethod。而且它不工作我挺想要的方式。它的工作原理,如果我接受输入作为单独的参数:

I am trying to pass in some simple JSON to an ASP.NET 4.5 Webmethod from jQuery. And it is not working quite the way I want it. It works if I accept the inputs as separate parameters:

[WebMethod]
public static Address GetJSonAddress(string name, string street)

但是,如果我试图把它作为一个对象不起作用,什么获取传递简直是空:

But if I try to take it as an object it does not work, what gets passed in is simply null:

[WebMethod]
public static Address GetJSonAddress(Address newAddress)

我一直在使用DataContractJsonSerializer ...什么都没有尝试过的Webmethods,Pagemethods,WCF。 Address类与数据成员/ DataContract适当装饰。属性相匹配包括大小写。

I have tried Webmethods, Pagemethods, WCF using DataContractJsonSerializer...nothing. The Address class is decorated appropriately with Datamember/DataContract. The properties are matched including case.

jQuery的,在我所试图传递的数据,包括在Address对象包装它的所有的方式...如果我做到这一点比我有WEBMETHOD不叫,我得到错误500的其他方式:

The jQuery, in which I have tried all manner of passing in the data including wrapping it in an Address object...if I do it any other way than what I have the Webmethod is not called and I get error 500:

Save2 = function () {
var address = { prefix: GLOBALS.curr_prefix };

$('input[id^=' + GLOBALS.curr_prefix + '],select[id^=' + GLOBALS.curr_prefix + ']').each(function () {
       address[this.id.substr(4)] = $.trim($(this).val());
})

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/WebServices/Insert",
    data: JSON.stringify(address),
    dataType: "json",
    success: function (data, textStatus) {
        console.log(data, textStatus);
    },
    failure: function (errMsg) {
        MsgDialog(errMsg);
    }
});
}

最后,我将与121的输入字符串要做到这一点,真的不希望有121参数的方法。任何帮助是AP preciated。

Eventually I will have to do this with 121 input strings, and really don't want to have a method with 121 parameters. Any help is appreciated.

推荐答案

我迅速成立了这个项目,我是能够成功地叫我的Web方法,请调整你的code相应。请确保您的类属性名相同为您传递通过JavaScript的人。

I quickly set up this project and I was able to successfully call my web method, please adjust your code accordingly. Make sure your class property names are the same as the ones that you pass through JavaScript.

web服务

    public static Contact getContact(Contact cnt)
    {
        cnt.name = "Abijeet Patro";
        cnt.phone = "Blah Blah";
        return cnt;
    }

的JavaScript / jQuery的

    $(document).ready(function () {
        var cnt = {name:'Hello',phone:'Hello'};
        $.ajax({
            type: "POST",
            url: "/Default.aspx/getContact",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({'cnt':cnt}), // Check this call.
            success: function (data) {
                debugger;
            }
        });
    });

public class Contact
{
    public string name { get; set; }
    public string phone { get; set; }
}



您可以在这里抓住从项目。另外,请使用Fiddler或Chrome监测AJAX请求/响应。我已经添加的图像显示如何监视使用Chrome AJAX请求。提琴手是更好和更详细。

You can grab the project from here. Also please use fiddler or Chrome to monitor AJAX requests/responses. I've added an image to show how to monitor AJAX requests using Chrome. Fiddler is even better and more detailed.

这篇关于从jQuery的传递一个用户定义的对象ASP.NET的WebMethod,使用JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆