0

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?

Brian Ray
  • 1,482
  • 3
  • 20
  • 20
Omri
  • 1,436
  • 7
  • 31
  • 61
  • Is the URL correct? Going on the name of the function, shouldn't it be `/url/postJson`? – Brian Ray Mar 16 '16 at 13:55
  • @BrianRay I fixed it. It was a typo. These are not the real names. Anyway, the real URL is correct. It was the first thing i've checked. – Omri Mar 16 '16 at 14:04
  • Well, it has something to do with the URL. That's the only way you're going to get a 404. However, without knowing the directory structure, there's not much more I can do to help. – Brian Ray Mar 16 '16 at 14:08
  • When i try browse directly to this URL, i get `HTTPError: (404, 'Missing parameters: data')` so i guess that the URL is not the issue. – Omri Mar 16 '16 at 14:10
  • Browser used GET method, not POST. – Stan Zeez Mar 16 '16 at 14:14
  • To check the URL by POST using CURL, for example. – Stan Zeez Mar 16 '16 at 14:16
  • @StanZeez i tried to use CURL as you posted below and got `HTTPError: (404, 'Missing parameters: data')`. Any idea why? – Omri Mar 16 '16 at 14:35
  • I've edited my post and added the server side function. – Omri Mar 16 '16 at 14:37
  • May be argument 'data' should be in sending data? (--data '{"data": "something", a: "500000", b: "4"}'). Do you have any specification for request to server? – Stan Zeez Mar 16 '16 at 14:40
  • @StanZeez I don't have any specification. I just need to post json on this URL. – Omri Mar 16 '16 at 14:51

3 Answers3

0

404 is state from server side. For checking exists and access your url try to curl:

curl -X POST -H "Content-Type: application/json" --data '{a: "500000", b: "4"}' http://url/portJson

My often used 100% working function for POST Django backend (Im not set 'contentType' and 'dataType', but I set 'X-CSRFToken'):

function sendAjaxRequest(url, data, done, always, fail, reqType, timeout) {
    var _done = function() {};
    var _always = function() {};
    var _fail = function(jqXHR,status,err){
        alert('Error (' + status + '): ' + err + '\n' + jqXHR.responseText);
    }
    var _reqType ='POST'
    var _timeout = 60000;

    if (typeof done !== 'undefined') _done = done;
    if (typeof always !== 'undefined') _always = always;
    if (typeof fail !== 'undefined') _fail = fail;
    if (typeof reqType !== 'undefined') _reqType = reqType;
    if (typeof timeout !== 'undefined') _timeout = timeout;

    var csrftoken = $.cookie('csrftoken');

    $.ajax({
        url: url,
        headers:{ 'X-CSRFToken': csrftoken },
        data: data,
        cache: false,
        type: _reqType,
        timeout: _timeout
    }).done(_done).always(_always).fail(_fail);
};

using:

var data = {'a': "5000", 'b': "4"};

sendAjaxRequest(
    "/my/url/",
    data,
    function(response, textStatus, jqXHR) {// done
       // do work
    }, undefined, undefined, 'POST'
);
Stan Zeez
  • 1,138
  • 2
  • 16
  • 38
0

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"}}
Community
  • 1
  • 1
Brian Ray
  • 1,482
  • 3
  • 20
  • 20
0

On the server change:

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

to:

@cherrypy.expose
@cherrypy.tools.json_in
def postJson(self):
    data = cherrypy.request.json
    print data //just for the test
cyraxjoe
  • 5,661
  • 3
  • 28
  • 42