boden
boden

Reputation: 1681

Why AJAX POST request failed?

I have simple a html page with form. When I do POST request then receive failed, but it not response from server. What I do wrong?

function createRequest(url, body) {
    var response;
    console.log(JSON.stringify(body)); // (1)
    $.ajax({
        url: "creation/" + url,
        type: "POST",
        async: false,
        contentType: 'application/json',
        data: JSON.stringify(body),
        success: function (data) {
            if (data.status == true) {
                response = data.response;
            } else {
                BootstrapDialog.show({
                    title: 'Error',
                    message: data.errorMessage
                });
            }
        }
    });
    return response;
}

Body (1)

{"name":"Test UI","bid":"2","budget":"20"}

When I do this request via POSTMAN all work fine.

jQuery

Error appaear in Chroome Version 57.0.2987.98 (64-bit), but in Edge all works fine enter image description here

enter image description here

Upvotes: 1

Views: 3022

Answers (1)

Jonathan Portorreal
Jonathan Portorreal

Reputation: 2836

Since there isn't much information you have shared; here are some tips you could use to debug your problem based of assumptions where I think the problem is at.

  1. debug your code by using a console.log(data) as the first thing in your code to make sure if you're accessing the right fields.
  2. data.status is usually a status code 1xx,2xx, 3xx, 4xx and 200 means the success of a request. I check if a response is successful like this data.status == 200, but you used data.status == true you should log the data to make sure you're using that correctly otherwise you'll always get a logged error.
  3. JSON.stringify(body) will convert the body into a string and the data field of an ajax call takes an object with parameters to pass in its request. Is JSON.stringify(body) really what you meant to do or is it just body?

Well if you decide to add more information I'll be sure to update the answer with additional suspicions.

Upvotes: 1

Related Questions