How to send JSON to Asp.NET Webservice?

I have a javascript object that I serialize using the JSON2 library. Then I try to pass this JSON string to the ASP.net web service. I changed the web method to try several different configurations of parameters, but all of them lead to the error "Internal Server Error" 500.

Can someone give me a hint?

 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

Web service:

 [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
    }
}
+3
source share
2 answers

I understood.

Step 1 . I wrapped the JS object in another object that contains a property that matches the parameter name in the web method. Notice the quotes around

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

Step 2 Then I serialize this new 'wrapper' object using JSON.stringify ().

3 - json. - ""

 public string StoreDataOut(object json)
    {

    }
+3

- .

:


   $.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"
    });
+2

Source: https://habr.com/ru/post/1794487/


All Articles