6

Ajax および ASP.NET WebMethods を使用して JSON オブジェクトを渡す際に問題があります。

function setStudentInfo() {
    var jsonObjects = [
        { id: 1, name: "mike" },
        { id: 2, name: "kile" },
        { id: 3, name: "brian" },
        { id: 1, name: "tom" }
    ];

    $.ajax({
        type: "POST",
        url: "ConfigureManager.aspx/SetStudentInfo",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        data: { students: JSON.stringify(jsonObjects) },
        success: function (result) {
            alert('success');
        },
        error: function (result) {
            alert(result.responseText);
        }

    });
}

ASP.NET コード

[WebMethod]
public static void SetStudentInfo(object students)
{
     //Here I want to iterate the 4 objects and to print their name and id
}

次のエラーが表示されます。

"{"Message":"無効な JSON プリミティブ: 学生。"、"StackTrace":" System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() で System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 深度) System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize (文字列入力、Int32 depthLimit、JavaScriptSerializer シリアライザー) で System.Web.Script.Serialization.JavaScriptSerializer.Deserialize (JavaScriptSerializer シリアライザー、文字列入力、型の種類、Int32 depthLimit) システムで。 System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext コンテキスト、JavaScriptSerializer シリアライザー) で System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext コンテキスト) で System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext コンテキスト, WebServiceMethodData methodData)","ExceptionType":"System. ArgumentException"}"

4

5 に答える 5

6

次のように、JSON全体を文字列として渡します。

data: '{variable: "value"}'

あなたが持っているようにそれを渡そうとすると、私はいつもエラーを受け取ります。

于 2012-11-19T07:16:35.877 に答える
3

単一のオブジェクトが期待されているが、コードはオブジェクトのリストを期待しているため、そのエラーが発生しています。これは、少し難しい場合があります。データ転送を簡単にするために、WebMethod に渡したいオブジェクトのタイプのプロパティを持つクラスを作成する必要があります。これは、ASP.NET が解析しやすいように思われるためです。例えば:

public class Student
{
    private string _name;
    private int _id;
    public string name {
        get { return _name; }
        set { _name = value; }
    }
    public int id {
        get { return _id; }
        set { _id = value; }
    }
}

そして、WebMethod は次のように、Student クラス オブジェクトのリストを受け入れるように変更されます。

[WebMethod]
public static void SetStudentInfo(List<Student> Students)
{
    foreach (Student s in Students)
    {
        //Do whatever here System.Console.WriteLine(s.name);
    }
}

あとは、次のように Ajax 呼び出しを少し変更するだけです。

function setStudentInfo() {
    var jsonObjects = [
        { id: 1, name: "mike" },
        { id: 2, name: "kile" },
        { id: 3, name: "brian" },
        { id: 1, name: "tom" }
    ];

    $.ajax({
        type: "POST",
        url: "ConfigureManager.aspx/SetStudentInfo",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        data: { Students: JSON.stringify(jsonObjects) },
        success: function (result) {
            alert('success');
        },
        error: function (result) {
            alert(result.responseText);
        }

    });
}
于 2012-11-19T11:48:58.300 に答える
1

このコードを試してください:

function setStudentInfo() {

var jsonObjects = {"students" : [
    { id: 1, name: "mike" },
    { id: 2, name: "kile" },
    { id: 3, name: "brian" },
    { id: 1, name: "tom" }
]};

$.ajax({
    type: "POST",
    url: "ConfigureManager.aspx/SetStudentInfo",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: false,
    data: JSON.stringify(jsonObjects),
    success: function (result) {
        alert('success');
    },
    error: function (result) {
        alert(result.responseText);
    }

});

}

于 2013-10-07T09:29:24.820 に答える
0

実際にはjsonをサーバーに送信していません。jsonを送信するには、dataプロパティにjson文字列を渡すだけです。

data: JSON.stringify(jsonObjects),
于 2012-11-19T07:16:17.717 に答える