skip to Main Content

At JSON.stringify , my javascript is giving the following message in the console.

parsererrorSyntaxError: Unexpected end of JSON input

var contentData =     {
                "Location": {
                    "City": "Bangalore Urban",
                    "State": "Karnataka",
                    "Country": "IN"
                },
                "Packages": [
                    "3 days and 4 nights"
                ],
                "Hotels": "Vividus",
                "Offers": [
                    "Enjoy up to 20% Savings",
                    "Hotel Discounts, Just for Booking Early.2 nights B&B to include first night dinner"
                ],
                "Activities": [
                    "scuba diving",
                    "swimming"
                ]
            }




        $.ajax({
            type : 'POST',
            url : '/my/reco/pkg.',
            data : JSON.stringify(contentData),
            async : false,    // I get this message both with false and true here.
            contentType : "application/json; charset=utf-8",
            success : function(data) {
                 console.log(data);

            },
            error : function(jqXHR, textStatus, message) {
                console.log("error"+jqXHR+textStatus+message);
            }
        });

When I put a debugger on in chrome, I see the error at the point JSON.stringify.

I have validated both json and the javascript code, both seems to be syntactically correct.

I can find the request json in the network tab under header section of chrome.

What am I doing wrong or missing here ?

2

Answers


  1. the error you don’t assign the object to variable contentData

    put at first var contentData =

    var contentData = {
      "Location": {
        "City": "Bangalore Urban",
        "State": "Karnataka",
        "Country": "IN"
      },
      "Packages": [
        "3 days and 4 nights"
      ],
      "Hotels": "Vividus",
      "Offers": [
        "Enjoy up to 20% Savings",
        "Hotel Discounts, Just for Booking Early.2 nights B&B to include first night dinner"
      ],
      "Activities": [
        "scuba diving",
        "swimming"
      ]
    }
    
    
    
    console.log(JSON.stringify(contentData));
    Login or Signup to reply.
  2. You do not need to do JSON.stringify in JSON.stringify(contentData). You can just send an object. So, send data without it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search