Jquery Ajax Posting json to webservice

后端 未结 6 482
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 06:15

I am trying to post a JSON object to a asp.net webservice.

My json looks like this:

var markers = { \"markers\": [
  { \"position\": \"128.3657142857         


        
6条回答
  •  囚心锁ツ
    2020-11-22 06:45

    I tried Dave Ward's solution. The data part was not being sent from the browser in the payload part of the post request as the contentType is set to "application/json". Once I removed this line everything worked great.

    var markers = [{ "position": "128.3657142857143", "markerPosition": "7" },
    
                   { "position": "235.1944023323615", "markerPosition": "19" },
    
                   { "position": "42.5978231292517", "markerPosition": "-3" }];
    
    $.ajax({
    
        type: "POST",
        url: "/webservices/PodcastService.asmx/CreateMarkers",
        // The key needs to match your method's input parameter (case-sensitive).
        data: JSON.stringify({ Markers: markers }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data){alert(data);},
        failure: function(errMsg) {
            alert(errMsg);
        }
    });
    

提交回复
热议问题