Invalid JSON string Error transferring JSON from AngularJS

I am trying to pass a JSON string in an ajax request. This is my code.

    NewOrder =  JSON.stringify (NewOrder);
    alert (NewOrder);

    var req = {
        url: '/cgi-bin/PlaceOrder.pl',
        method: 'POST',
        headers: { 'Content-Type': 'application/json'},
        data: "mydata="+ NewOrder
    };  

    $http(req)
    .success(function (data, status, headers, config) {
        alert ('success');
    })
    .error(function (data, status, headers, config) {
        alert (status);
        alert (data);
        alert ('Error')
    });

      

alert (NewOrder) gives -

{"ItemList":[{"ItemName":"Quality Plus Pure Besan 500 GM","Quantity":1,"MRP":"28.00","SellPrice":"25.00"}],"CustomerID":1,"DeliverySlot":2,"PaymentMode":1}

      

This seems to be a valid JSON string.

But aside from the script I am getting the following error. in this line

my $decdata = decode_json($cgi->param('mydata'));

      

invalid JSON string, neither array, nor object, nor number, nor string, nor atom, at character offset 0 (before "end of line")

Can anyone help me why I am getting this error?

+3


source to share


3 answers


$cgi->param('myData')

returns a string of query parameters 'mydata' which is not sent in your case.

You are sending json data in the request body of your post payload, not as a query / form parameter. In this case, you will need another function to read the content of the request body on the server side script.

which happens: my $data = $query->param('POSTDATA');

as described here: http://search.cpan.org/~lds/CGI.pm-3.43/CGI.pm#HANDLING_NON-URLENCODED_ARGUMENTS

Also you have to remove "mydata =" from your json in the body you are posting because HTTP request bodies do not have parameter names (they are only for query / form-params parameters).



Your final code should look like this:

var req = {
    url: '/cgi-bin/PlaceOrder.pl',
    method: 'POST',
    headers: { 'Content-Type': 'application/json'},
    data: NewOrder
}; 

      

and on the server side:

my $decdata = decode_json($query->param('POSTDATA'));

      

+3


source


I think it might be related to this issue: AngularJs $ http.post () is not sending data

Usually I would post the data like this:

var req = {
    url: '/cgi-bin/PlaceOrder.pl',
    method: 'POST',
    headers: { 'Content-Type': 'application/json'},
    data: {"mydata" : NewOrder}
};  

      



However, I am assuming that you are expecting data as query parameters from this:

my $decdata = decode_json($cgi->param('mydata'));

      

If so, then the related SO question is what you are looking for.

0


source


Angular $ http.post takes two parameters as url and payload

   var url = '/cgi-bin/PlaceOrder.pl';
   var payLoad = {'myData' :JSON.stringify(NewOrder)}; 

    $http.post(url, payLoad)
    .success(function(data) {
    console.log(success);
    })

      

Server side by fetching the required json string from the request parameter and then run the json like this:

    $result = $cgi->param('myData');
    my $decdata = decode_json($result);

      

0


source







All Articles