簡體   English   中英

如何將JSON發送到Asp.NET Webservice?

[英]How do I send JSON to Asp.NET Webservice?

我有一個javascript對象,我正在使用JSON2庫進行序列化。 然后我嘗試將此JSON字符串傳遞給ASP.net Web服務。 我已經改變了webmethod來嘗試幾種不同的參數配置,但它們都會導致'500 - 內部服務器錯誤'

有人能給我一個線索嗎?

 function postDataToService(data) {
    $.ajax({
        url: "http://localhost:2686/DataCollectionService.asmx/StoreDataOut",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: data,
        success: showSuccessNotice,
        error: showFailureNotice,
        dataType: "json"
    });

} //postdatatoservice

function convertDataToJSON(jsObj) {

    return JSON.stringify({ list: jsObj });

} //converdatatojson

網絡服務:

 [WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class DataCollectionService : WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string StoreDataOut(List<string> list)
    {
        return "Complete";
        //model functionality
    }
}

我知道了..

第1步 :我將JS對象包裝在另一個對象中,該對象包含與web方法中的參數名稱匹配的屬性。 通知周圍的報價

return JSON.stringify({'json':jsObj});

步驟2然后我用JSON.stringify()序列化了這個新的'wrapper'對象。

步驟3 Web方法的參數名稱匹配已發布的json屬性名稱。 類型是'對象'

 public string StoreDataOut(object json)
    {

    }

我使用了您提供的代碼,並且能夠毫無問題地發布到Web服務。

一些問題:

  1. 數據是什么樣的?
  2. 什么是500服務器錯誤? 您可以直接瀏覽網絡服務而不會出錯嗎? HTTP://本地主機:2686 / DataCollectionService.asmx / StoreDataOut
  3. 我注意到你的數據發送給你從來沒有調用convertDataToJSON()這是一個錯字? 如果不執行如下的ajax帖子:


   $.ajax({
            url: "http://localhost:2686/DataCollectionService.asmx/StoreDataOut",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: convertDataToJSON(data),
            success: showSuccessNotice,
            error: showFailureNotice,
            dataType: "json"
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM