weixin_33747129 2017-03-10 09:56 采纳率: 0%
浏览 44

为什么AJAX POST请求失败?

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

  • 写回答

1条回答 默认 最新

  • weixin_33736649 2017-03-10 10:22
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么