Jquery Ajax Posting JSON to webservice

587,435

Solution 1

You mentioned using json2.js to stringify your data, but the POSTed data appears to be URLEncoded JSON You may have already seen it, but this post about the invalid JSON primitive covers why the JSON is being URLEncoded.

I'd advise against passing a raw, manually-serialized JSON string into your method. ASP.NET is going to automatically JSON deserialize the request's POST data, so if you're manually serializing and sending a JSON string to ASP.NET, you'll actually end up having to JSON serialize your JSON serialized string.

I'd suggest something more along these lines:

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);},
    error: function(errMsg) {
        alert(errMsg);
    }
});

The key to avoiding the invalid JSON primitive issue is to pass jQuery a JSON string for the data parameter, not a JavaScript object, so that jQuery doesn't attempt to URLEncode your data.

On the server-side, match your method's input parameters to the shape of the data you're passing in:

public class Marker
{
  public decimal position { get; set; }
  public int markerPosition { get; set; }
}

[WebMethod]
public string CreateMarkers(List<Marker> Markers)
{
  return "Received " + Markers.Count + " markers.";
}

You can also accept an array, like Marker[] Markers, if you prefer. The deserializer that ASMX ScriptServices uses (JavaScriptSerializer) is pretty flexible, and will do what it can to convert your input data into the server-side type you specify.

Solution 2

  1. markers is not a JSON object. It is a normal JavaScript object.
  2. Read about the data: option:

    Data to be sent to the server. It is converted to a query string, if not already a string.

If you want to send the data as JSON, you have to encode it first:

data: {markers: JSON.stringify(markers)}

jQuery does not convert objects or arrays to JSON automatically.


But I assume the error message comes from interpreting the response of the service. The text you send back is not JSON. JSON strings have to be enclosed in double quotes. So you'd have to do:

return "\"received markers\"";

I'm not sure if your actual problem is sending or receiving the data.

Solution 3

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);
    }
});

Solution 4

I have encountered this one too and this is my solution.

If you are encountering an invalid json object exception when parsing data, even though you know that your json string is correct, stringify the data you received in your ajax code before parsing it to JSON:

$.post(CONTEXT+"servlet/capture",{
        yesTransactionId : yesTransactionId, 
        productOfferId : productOfferId
        },
        function(data){
            try{
                var trimData = $.trim(JSON.stringify(data));
                var obj      = $.parseJSON(trimData);
                if(obj.success == 'true'){ 
                    //some codes ...
Share:
587,435

Related videos on Youtube

Code Pharaoh
Author by

Code Pharaoh

I am a web developer with over 10 years experience in developing websites for both the private and public sector. I have been involved in a number of high profile and award winning projects including: The Gallery of Lost Art and the redesign and development of the Hunterian Art Gallery.

Updated on April 02, 2021

Comments

  • Code Pharaoh
    Code Pharaoh about 3 years

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

    My json looks like this:

    var markers = { "markers": [
      { "position": "128.3657142857143", "markerPosition": "7" },
      { "position": "235.1944023323615", "markerPosition": "19" },
      { "position": "42.5978231292517", "markerPosition": "-3" }
    ]};
    

    I am using the json2.js to stringyfy my JSON object.

    and I am using jquery to post it to my webservice.

      $.ajax({
            type: "POST",
            url: "/webservices/PodcastService.asmx/CreateMarkers",
            data: markers,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data){alert(data);},
            failure: function(errMsg) {
                alert(errMsg);
            }
      });
    

    I am getting the following error:

    Invalid JSON primitive

    I have found a bunch of posts relating to this and it seems to be a really common problem but nothing i try fixes the issue.

    When firebug what is being posted to the server it looks like this:

    markers%5B0%5D%5Bposition%5D=128.3657142857143&markers%5B0%5D%5BmarkerPosition%5D=7&markers%5B1%5D%5Bposition%5D=235.1944023323615&markers%5B1%5D%5BmarkerPosition%5D=19&markers%5B2%5D%5Bposition%5D=42.5978231292517&markers%5B2%5D%5BmarkerPosition%5D=-3

    My webservice function that is being called is:

    [WebMethod]
    public string CreateMarkers(string markerArray)
    {
        return "received markers";
    }
    
    • danicotra
      danicotra over 10 years
      'failure' is not provided as a possible setting amongst those listed in api.jquery.com/jQuery.ajax ...maybe you mistaken it with 'error' instead?
  • Code Pharaoh
    Code Pharaoh almost 13 years
    Thanks for you help Felix, I thought that by running markers through the JSON.stringyfy method i was converting it to a query string, I have done as you suggested but unfortunatly it does not work I am not posting the following.
  • Code Pharaoh
    Code Pharaoh almost 13 years
    markers=%7B%22markers%22%3A%5B%7B%22position%22%3A%22128.365‌​7142857143%22%2C%22m‌​arkerPosition%22%3A%‌​227%22%7D%2C%7B%22po‌​sition%22%3A%22235.1‌​944023323615%22%2C%2‌​2markerPosition%22%3‌​A%2219%22%7D%2C%7B%2‌​2position%22%3A%2242‌​.5978231292517%22%2C‌​%22markerPosition%22‌​%3A%22-3%22%7D%5D%7D
  • Felix Kling
    Felix Kling almost 13 years
    @Dreamguts: It is a bit unclear to me what you want. Do you want to send markers as JSON string?
  • Code Pharaoh
    Code Pharaoh almost 13 years
    Hi Felix, yes i want to send the markers object as a JSON string so that i can use it in my webservice.
  • Felix Kling
    Felix Kling almost 13 years
    @Dreamguts: Then it should work this way. Also the "code" you posted in the comment looks ok. Of course you have to decode it properly on the server side and maybe you have to change the name of the parameter, I don't know.
  • Code Pharaoh
    Code Pharaoh almost 13 years
    just read your edit and i am pretty sure that it is the markers string that is causing the error as i get "Invalid JSON primitive: markers.
  • Felix Kling
    Felix Kling almost 13 years
    @Dreamguts: Maybe you need: data: {markers: JSON.stringify(markers.markers)}
  • Code Pharaoh
    Code Pharaoh almost 13 years
    @Felix Kling: yeah i thought that too but doesnt seem to have worked.
  • Felix Kling
    Felix Kling almost 13 years
    @Dreamguts: Well, I cannot say more without more information. Unfortunately I have no experience with asp.net.
  • Softlion
    Softlion over 12 years
    You wrote "so that jQuery doesn't attempt to URLEncode your data.", but it is not correct. To stop jquery from urlencoding your data you must set processData to false.
  • rlorenzo
    rlorenzo over 12 years
    Passing in a string is enough to prevent jQuery from URLEncoding the data parameter. You can set processData to false, but it's superfluous (and doing that alone, without passing in a JSON string for the data, isn't enough).
  • GregT
    GregT over 11 years
    This same issue (and answer) applies to Rails as well.
  • Royi Namir
    Royi Namir almost 11 years
    @DaveWard Does contentType is considered if we use GET and not POST ?
  • rlorenzo
    rlorenzo almost 11 years
    @RoyiNamir If you're using ASMX ScriptServices or ASPX WebMethods, you need to use POST in order to get them to return JSON. If you GET, Content-Type correct or not, you'll get XML instead.
  • danicotra
    danicotra over 10 years
    @DaveWard I just can't find the 'failure' setting listed in api.jquery.com/jQuery.ajax I think the answerer mistaken it with 'error' instead
  • TrueWill
    TrueWill over 10 years
    With ASP.NET Web API it appears that the Markers key (the extra level of JSON hierarchy) should not be used (although I didn't try it with a collection parameter). Thank you - your answer resolved hours of trial and error!
  • Hitesh
    Hitesh about 10 years
    Do I have to do JSON.??stringify Since from above I can clearly see that it is in JSON format
  • rlorenzo
    rlorenzo about 10 years
    @hitesh: markers is an object literal until you use JSON.stringify on it, not JSON. If you pass jQuery an object literal as the data parameter to $.ajax, it sends the data as a URL encoded string, not a JSON string.
  • Ravi Panchal
    Ravi Panchal over 8 years
    here how to list object pass into json to the ajax call of java webservice.
  • Ranger
    Ranger about 7 years
    Dave, your links to encosia.com are currently reported by Chrome as distributing malware.
  • halfer
    halfer over 6 years
    After completing a CAPTCHA challenge at Cloudflare, Encosia is giving me "Error 522 - Connection timed out".
  • n1nsa1d00
    n1nsa1d00 about 6 years
    Use file_get_contents('php://input') to get your json data in php.
  • Iman Estiri
    Iman Estiri about 3 years
    This Did not Work For me . i tried Your Code but The Parameters is Null on Server Side . and i get Null Reference Error . @DaveWard
  • sekrett
    sekrett almost 3 years
    I am working with a server, I don't have access to. I removed contentType, it helped. But the payload was passing with and without it. Something else changed. The contentType changed to "application/x-www-form-urlencoded", which is wrong, but it works. Magic. Thanks.
  • jhenya-d
    jhenya-d over 2 years
    not working with headers: { 'X-Requested-With': 'XMLHttpRequest' },