jQuery, JSON objects and .NET WebServices

本文关键字:NET WebServices and objects JSON jQuery | 更新日期: 2023-09-27 18:27:28

我有一个问题,关于从JQuery创建JSON,通过Ajax将其发送到.NET中的Web服务,以及如何在.NET中反序列化并保存到数据库。

也许有人可以帮我看看我错在哪里了!

jQuery和JSON:

var myObject = {};
//** BusinessPartner **
//function BusinessPartner(prefix) {
//    this.bpCodigo = $(prefix + " option:selected").val();
//    this.bpNombre = $(prefix + "-infonombre").text();
//
//** Locations **
//function Locations(prefix) {
//    this.locCode = $(prefix + " option:selected").val();
//    this.locName = $(prefix + "-name").text();
var oNewObject = new BusinessPartner("#bp-01");
var oNewObject2 = new BusinessPartner("#bp-02");
var myLocation = [];
var oOrigin = new Locations("#receipt");
myLocation.push(oOrigin);
var oDestination = new Locations("#portloading");
myLocation.push(oOrigin);
myObject["Agent1"] = oNewObject;
myObject["Agent2"] = oNewObject;
myObject["Locations"] = myLocation;
$.ajax({
    type: "POST",
    url: "Services/ActionsDb.asmx/saveData",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(myObject),
    datatype: "json",
    success: function (response) {
        // After correct save send OK message.
    },
    error: function (xhr, status) {
        alert("An error occurred: " + status);
    }
});

C#中的类

据我所知,它应该与Web服务接收到的JSON格式完全相同。

public class Agent
{
    public string bpCodigo { get; set; }
    public string bpNombre { get; set; }
}
public class Location 
{
    public string locCode { get; set; }
    public string locName { get; set; }
}
public class oGet
{
    public Agent oCliente { get; set; }
    public Agent oCliente2 { get; set; }
    public List<Location> oLocations { get; set; }
}

Web服务

很好,现在WebServiceActionsDb.asmx/savedata将被要求将数据保存在DB中。

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string saveData(oGet myObject)
    {
        try
        {
            string sClienteName = myObject.oCliente.bpNombre.ToString();
            return sClienteName.ToString();
        }
        catch (System.Exception ex)
        {
            return ex.Message.ToString();
        }
    }

结果

我得到的错误如下:

POST http://localhost:8869/Services/WebtoolActionsDb.asmx/saveBLData 500 (Internal Server Error)    
{"Message":"Service call not valid. Parameter missing: 'u0027myObject'u0027.","StackTrace":" 
en System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters) 
en System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters) 
en System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
en System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

jQuery, JSON objects and .NET WebServices

您的JavaScript有两个问题。出现500错误的原因是,您发送的JSON需要有一个"myObject"属性才能与saveData的参数相对应。为了匹配oGet中的属性,您还需要将"Agent1"更改为"oCliente",将"Agent2"更改为《oClient2》,将"Locations"更改为「oLocations」。

myObject.oCliente = oNewObject;
myObject.oCliente2 = oNewObject2;
myObject.oLocations = myLocation;
$.ajax({
    // ...
    data: JSON.stringify({ myObject: myObject }),
    // ...
});