【问题标题】:Why doesn't jquery call the success handler in this case?在这种情况下,为什么 jquery 不调用成功处理程序?
【发布时间】:2016-06-27 14:16:35
【问题描述】:

我有这个代码:

$.post( "/ankauf/", {
        "kunden_id" : 1,
        "products" : products,
        "full_price" : parseInt($('#totalPrice').text()),
        "_token" : $('meta[name="csrf-token"]').attr('content')
    },
    function( data ) {            
        toastr.success("Ankauf abgeschlossen", "OK!");
    }
);

就像我在应用程序中经常做的那样,它会向我的服务器发送一个发布请求。

Chrome 会这样显示这个请求:

Request Method:POST  
Status Code:200 OK  
Remote Address:192.168.178.80:1414  

响应如下所示:

Cache-Control:no-cache  
Connection:close  
Content-Type:application/json

Response content:  
array(7) {  
  ["product_id"]=>  
  string(1) "5"  
  ["paidprice"]=>  
  string(2) "85"  
  ["condition"]=>  
  string(8) "Sehr Gut"  
  ["ovp1"]=>  
  string(1) "0"  
  ["ovp2"]=>  
  string(1) "0"  
  ["ovp3"]=>  
  string(1) "0"  
  ["ovp4"]=>  
  string(1) "0"  
}  
{"full_price":"85","updated_at":"2016-06-27 14:01:55","created_at":"2016-06-27 14:01:55","id":73,"created_by_id":1,"customer_id":1}

所以服务器响应看起来像一个有效的 JSON 响应,并且 http-code 是 200 any Ideas 为什么成功处理程序不会触发?

【问题讨论】:

    标签: javascript jquery json post


    【解决方案1】:

    这是有效的 JSON:

    {"full_price":"85","updated_at":"2016-06-27 14:01:55","created_at":"2016-06-27 14:01:55","id":73,"created_by_id":1,"customer_id":1}
    

    但这不是:

    array(7) {
    ["product_id"]=>
    string(1) "5"
    ["paidprice"]=>
    string(2) "85"
    ["condition"]=>
    string(8) "Sehr Gut"
    ["ovp1"]=>
    string(1) "0"
    ["ovp2"]=>
    string(1) "0"
    ["ovp3"]=>
    string(1) "0"
    ["ovp4"]=>
    string(1) "0"
    }
    {"full_price":"85","updated_at":"2016-06-27 14:01:55","created_at":"2016-06-27
    14:01:55","id":73,"created_by_id":1,"customer_id":1}
    

    看起来服务器正在使用 PHP,并且在打印 JSON 的语句之前有一个 print_r

    【讨论】:

    • php 代码中有一个 var_dump。我认为这是一些由 chrome-developer-tools 格式化的东西。谢谢你的帮助。将在 8 分钟内接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多