Search code examples
jqueryjsonajaxcherrypy

Posting json with ajax issue


I have the following code, which always generate 404 error (not found):

data = Object {a: "500000", b: "4"}
postJson(data);


function postJson(data){
    $.ajax({
                url: '/url/postJson',
                type: 'POST',
                data: data, //also tried "JSON.stringify(data)"
                dataType: "json",
                contentType: "application/json",
                success: function (data, textStatus, jqXHR) {

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert('error')
                }
            });
}

On the server side:

@cherrypy.expose
def postJson(self, data):
    print data //just for the test

What could be the issue?


Solution

  • Going by the code in this answer, it looks like your JSON formatting needs to be updated. Try this.

    data = {data: {a: "500000", b: "4"}}