JSON,AJAX和ASP.NET [英] JSON, AJAX, and ASP.NET

查看:119
本文介绍了JSON,AJAX和ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户端文件在其jQuery中有一个ajax调用,该调用将JSON数据发送到服务器端WebMethod.以下代码有效:

My client-side file has an ajax call in its jQuery which sends JSON data to a server-side WebMethod. The following code works:

服务器端(C#)上的WebMethod

WebMethod on server-side (C#)

[System.Web.Services.WebMethod]
public static string GetCurrentTime(string[] name)
{
    string str = "";
    foreach(string s in name)
    {
        str += s;
    }
    return str;
}

和ajax调用(jQuery):

and the ajax call (jQuery):

        //array = ["a","b","c"]
        //data = {'name':["a","b","c"]}
        $.ajax({
            type: "POST",
            url: "schedule.aspx/GetCurrentTime",
            data: "{'name':" + JSON.stringify(array) + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response){
                alert(response.d);
            },
            complete: function () {
                alert("complete");
            }
        });

tl; dr 上面的代码采用JSON字符串,并在服务器端方法中创建字符串数组.

tl;dr The above code takes the JSON string and creates an array of strings in the server-side method.

如果将JSON更改为以下内容,我将如何重写上面的WebMethod?名称"不再是上面代码中的字符串数组,而是某些对象的数组,因此需要更改WebMethod中的参数类型,但不确定如何.

How would I rewrite the above WebMethod if I change the JSON to the following? "name" no longer is an array of strings as it was in the above code, but an array of some object, so the parameter type in the WebMethod would need to be changed but I'm not sure to what.

{"name":[{"value":"val1"},{"value":"val2"}]}

这是我执行ajax请求时在Chrome的网络"标签上收到的消息和堆栈跟踪:

This is the message and stacktrace I'm receiving on Chrome's Network tab when I execute the ajax request:

Message
:
"No parameterless constructor defined for type of 'System.String'."
StackTrace
:
"   at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
↵   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
↵   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
↵   at System.Web.Script.Serialization.ObjectConverter.AddItemToList(IList oldList, IList newList, Type elementType, JavaScriptSerializer serializer, Boolean throwOnError)
↵   at System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer serializer, Boolean throwOnError, IList& convertedList)
↵   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
↵   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
↵   at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)
↵   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)
↵   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
↵   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)"

推荐答案

如果要将复杂的对象发送到您的web方法中,例如. {"value":"val1"},那么您需要创建一个具有与JSON匹配的属性的C#类,以便能够接收数据.

If you want to send in a complex object to your webmethod eg. {"value":"val1"} then you need to create a C# class with properties matching your JSON to be able to receive the data.

因此,在您的情况下,您需要一个数据类,例如:

So in your case you need a data class, something like:

public class ValueObject
{
    public string Value { get; set; }
}

然后,您需要更改网络方法以接受ValueObject数组:

Then you need to change your webmethod to accept an array of ValueObject:

[System.Web.Services.WebMethod]
public static string GetCurrentTime(ValueObject[] name)
{
    string str = "";
    foreach (var s in name)
    {
        str += s.Value;
    }
    return str;
}

请注意,属性名称Value必须与JSON value

Note the property name Value has to match the property name in your JSON value

这篇关于JSON,AJAX和ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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