Abhishek
Abhishek

Reputation: 1031

Jquery Ajax Posting JSON to Server does not work

I am trying to post JSON data to server using JQuery but the server is not being hit if I set the content-type. there is no HTTP hit taking place. I checked via Firebug. If I remove the content-type then the server gets hit but the data is in the form of key-value pairs.

I tried using a HTTPClient in Java to do the test to hit on server and it works absolutely great. So there is no problem on the server.

function abc(){
    var abhishek = { 'latitude' : '123',
                     'longitude':'23'};
    $.ajax({
        url : "http://dev.reportaspot.com/health/jsonHit",
        //contentType: 'application/json',
        type : 'POST',
        data : JSON.stringify(abhishek),
        success : function(data2){
            alert('Load was performed.' + data2);
        },
        error : function(err,textstatus,errorThrown){
            console.log(err);
            console.log(textstatus);
            console.log(errorThrown);
            alert('Error '+err);
        }
    });



$(document).ready(function(){
    abc();
    var abhishek = { 'latitude' : '123',
             'longitude':'23'};
});

Attached JSFiddle code. http://jsfiddle.net/AynLX/3/

Upvotes: 0

Views: 394

Answers (1)

rmobis
rmobis

Reputation: 26992

If you're posting it, there's no need to stringify it. Simply do data: abhishek.

Upvotes: 1

Related Questions