jQuery: get order id from json response

This question already has an answer here:

  • Parse JSON in JavaScript? [duplicate] 16 answers
  • Safely turning a JSON string into an object 22 answers
  • Parse Json in javascript or jquery [duplicate] 7 answers

  • If response is a string, you might need to parse it, otherwise you can read the value directly.

    If console.log shows an object you can expand (in developer tools console) just use

    var id = response.order.id;
    

    If that doesn't work, or you're sure it's a string use

    var responseObj = JSON.parse(response);
    var id = responseObj.order.id;
    

    You need to ensure that response is parsed into an object before doing response.order.id

    Mention

    dataType : "json"
    

    ie

    $.ajax({
        url: '/v1/shopify-Ajax/ajax.php',
        dataType : "json",
        method: 'post',
        data: {datalog: dataLog, variant: $('#prod').val()}
        })
        .success( function(response){ console.log( response.order.id ) })
        //window.location.href = "/v1/thank-you.php";
    })
    

    在您的AJAX成功中,您需要解码JSON。

    var data = $.parseJSON(response);
    alert(data.order.id); //you will get order id
    
    链接地址: http://www.djcxy.com/p/31728.html

    上一篇: 将Json解析的字符串还原为Json

    下一篇: jQuery:从json响应获取订单ID