3

I am trying to pass JSON string in 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}

Which seems to be a valid JSON string.

But in script side I am getting following error. in this line

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

malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)")

Can please someone help me why i am getting this error?

1
  • 2
    Can you display on your server side $cgi->param('mydata') ? Commented Jun 28, 2015 at 11:44

3 Answers 3

3
+50

$cgi->param('myData') returns the query param string 'mydata', which in your case is not sent.

You're posting the json data in the request body of your http post payload, and not as a query/form param. In that case, you'd need some other function to read the contents of the request body in your server-side script.

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

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

Also you should remove the "mydata=" from your json in the body you post, because http request payload bodies do not have parameter names (they're for query/form-params only).

Your end code should be like this:

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

and the servers side:

my $decdata = decode_json($query->param('POSTDATA'));
1
  • Thanks neuro_sys. It works like a charm. I was struggling in this from couple of days. Your solution is just perfect. Thanks again Commented Jun 30, 2015 at 16:13
0

I think it may be related to this issue: AngularJs $http.post() does not send data

Usually I would post 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 the data as request params from this:

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

If that is the case then the linked SO question is what you are looking for.

1
  • I changed the code as per your suggestion. But still same issue. :( Commented Jun 28, 2015 at 11:50
0

Angular $http.post accepts two params 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);
    })

At the server side, while fetching the required json string from the request param and then desearlize the json as following:

    $result = $cgi->param('myData');
    my $decdata = decode_json($result);
4
  • Hi Dev-One. I tried your suggestion. But i am still getting the same error. Commented Jun 28, 2015 at 12:15
  • Can you share the server side function on how you are handling the request of the json object i.e $cgi-> param('myData') Commented Jun 28, 2015 at 12:17
  • I am handling like this: my $decdata = decode_json($cgi->param('myData')); Commented Jun 28, 2015 at 12:23
  • Please be informed i am using Perl (not php), that's why using function decode_json (not json_decode) Commented Jun 28, 2015 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.