Javascript jQuery:从json响应获取订单id

Javascript jQuery:从json响应获取订单id,javascript,jquery,Javascript,Jquery,我想获取json响应的值。我编写了一个Ajax函数,如下所示: $.ajax({ url: '/v1/shopify-Ajax/ajax.php', method: 'post', data: {datalog: dataLog, variant: $('#prod').val()} }) .success(function(response){ //window.location.href = "/v1/thank-you.php"; }) {

我想获取json响应的值。我编写了一个Ajax函数,如下所示:

$.ajax({
    url: '/v1/shopify-Ajax/ajax.php',
    method: 'post',
    data: {datalog: dataLog, variant: $('#prod').val()}
    })
    .success(function(response){
    //window.location.href = "/v1/thank-you.php";
})
{"order":
    {"id":303657910281,
     "email":"test20@code1.com",
     "closed_at":null,
     "created_at":"2018-03-19T01:04:58-07:00",
     "updated_at":"2018-03-19T01:04:58-07:00",
     "number":811,
     "note":null,
     "token":"9709d7c295ac1dbbaf29c2d09a9d5a9d",
     "gateway":"",
     "test":false,
     "total_price":"82.49",
     "subtotal_price":"82.49",
     "total_weight":null,
     "total_tax":"0.00",
     "taxes_included":false,
     "currency":"USD",
     "financial_status":"paid",
     "confirmed":true,          
     "total_discounts":"0.00",
     "total_line_items_price":"82.49","cart_token":null,"buyer_accepts_marketing":false,"name":"#1811","referring_site":null,"landing_site":null,"cancelled_at":null,"cancel_reason":null,"total_price_usd":"82.49","checkout_token":null,"reference":null,"user_id":null,"location_id":null,"source_identifier":null,"source_url":null,"processed_at":"2018-03-19T01:04:58-07:00","device_id":null,"phone":null,"customer_locale":null,"app_id":2306584,"browser_ip":null,"landing_site_ref":null,"order_number":1811,"discount_codes":[],"note_attributes":[],"payment_gateway_names":[""],"processing_method":"","checkout_id":null,"source_name":"2306584","fulfillment_status":null,"tax_lines":[],"tags":"","contact_email":"test20@code1.com","order_status_url":"https:\/\/checkout.shopify.com\/19258983\/orders\/9709d7c295ac1dbbaf29c2d09a9d5a9d\/authenticate?key=0XXX","line_items":[{"id":610330640393,"variant_id":2323256639497,"title":"XX","quantity":1,"price":"82.49","sku":"","variant_title":"3 XX","vendor":"X1","fulfillment_service":"manual","product_id":235965415433,"requires_shipping":false,"taxable":false,"gift_card":false,"pre_tax_price":"82.49","name":"XXXX","variant_inventory_management":null,"properties":[],"product_exists":true,"fulfillable_quantity":1,"grams":0,"total_discount":"0.00","fulfillment_status":null,"tax_lines":[]}],"shipping_lines":[],"billing_address":{"first_name":"","address1":"","phone":null,"city":"","zip":"","province":"","country":null,"last_name":"","address2":null,"company":null,"latitude":null,"longitude":null,"name":"","country_code":null,"province_code":null},"shipping_address":{"first_name":"test","address1":"6116 Beverly Dr","phone":null,"city":"Adair","zip":"50002","province":"Iowa","country":"United States","last_name":"tes","address2":null,"company":null,"latitude":null,"longitude":null,"name":"test tes","country_code":"US","province_code":"IA"},"fulfillments":[],"refunds":[],"customer":{"id":324158947337,"email":"test20@code1.com","accepts_marketing":false,"created_at":"2018-03-19T01:04:58-07:00","updated_at":"2018-03-19T01:04:58-07:00","first_name":"test","last_name":"tes","orders_count":1,"state":"disabled","total_spent":"0.00","last_order_id":303657910281,"note":null,"verified_email":true,"multipass_identifier":null,"tax_exempt":false,"phone":null,"tags":"","last_order_name":"#1811","default_address":{"id":371809157129,"customer_id":324158947337,"first_name":"test","last_name":"tes","company":null,"address1":"6116 Beverly Dr","address2":null,"city":"Adair","province":"Iowa","country":"United States","zip":"50002","phone":null,"name":"test tes","province_code":"IA","country_code":"US","country_name":"United States","default":true}}}}
我从服务器端脚本获得如下响应:

$.ajax({
    url: '/v1/shopify-Ajax/ajax.php',
    method: 'post',
    data: {datalog: dataLog, variant: $('#prod').val()}
    })
    .success(function(response){
    //window.location.href = "/v1/thank-you.php";
})
{"order":
    {"id":303657910281,
     "email":"test20@code1.com",
     "closed_at":null,
     "created_at":"2018-03-19T01:04:58-07:00",
     "updated_at":"2018-03-19T01:04:58-07:00",
     "number":811,
     "note":null,
     "token":"9709d7c295ac1dbbaf29c2d09a9d5a9d",
     "gateway":"",
     "test":false,
     "total_price":"82.49",
     "subtotal_price":"82.49",
     "total_weight":null,
     "total_tax":"0.00",
     "taxes_included":false,
     "currency":"USD",
     "financial_status":"paid",
     "confirmed":true,          
     "total_discounts":"0.00",
     "total_line_items_price":"82.49","cart_token":null,"buyer_accepts_marketing":false,"name":"#1811","referring_site":null,"landing_site":null,"cancelled_at":null,"cancel_reason":null,"total_price_usd":"82.49","checkout_token":null,"reference":null,"user_id":null,"location_id":null,"source_identifier":null,"source_url":null,"processed_at":"2018-03-19T01:04:58-07:00","device_id":null,"phone":null,"customer_locale":null,"app_id":2306584,"browser_ip":null,"landing_site_ref":null,"order_number":1811,"discount_codes":[],"note_attributes":[],"payment_gateway_names":[""],"processing_method":"","checkout_id":null,"source_name":"2306584","fulfillment_status":null,"tax_lines":[],"tags":"","contact_email":"test20@code1.com","order_status_url":"https:\/\/checkout.shopify.com\/19258983\/orders\/9709d7c295ac1dbbaf29c2d09a9d5a9d\/authenticate?key=0XXX","line_items":[{"id":610330640393,"variant_id":2323256639497,"title":"XX","quantity":1,"price":"82.49","sku":"","variant_title":"3 XX","vendor":"X1","fulfillment_service":"manual","product_id":235965415433,"requires_shipping":false,"taxable":false,"gift_card":false,"pre_tax_price":"82.49","name":"XXXX","variant_inventory_management":null,"properties":[],"product_exists":true,"fulfillable_quantity":1,"grams":0,"total_discount":"0.00","fulfillment_status":null,"tax_lines":[]}],"shipping_lines":[],"billing_address":{"first_name":"","address1":"","phone":null,"city":"","zip":"","province":"","country":null,"last_name":"","address2":null,"company":null,"latitude":null,"longitude":null,"name":"","country_code":null,"province_code":null},"shipping_address":{"first_name":"test","address1":"6116 Beverly Dr","phone":null,"city":"Adair","zip":"50002","province":"Iowa","country":"United States","last_name":"tes","address2":null,"company":null,"latitude":null,"longitude":null,"name":"test tes","country_code":"US","province_code":"IA"},"fulfillments":[],"refunds":[],"customer":{"id":324158947337,"email":"test20@code1.com","accepts_marketing":false,"created_at":"2018-03-19T01:04:58-07:00","updated_at":"2018-03-19T01:04:58-07:00","first_name":"test","last_name":"tes","orders_count":1,"state":"disabled","total_spent":"0.00","last_order_id":303657910281,"note":null,"verified_email":true,"multipass_identifier":null,"tax_exempt":false,"phone":null,"tags":"","last_order_name":"#1811","default_address":{"id":371809157129,"customer_id":324158947337,"first_name":"test","last_name":"tes","company":null,"address1":"6116 Beverly Dr","address2":null,"city":"Adair","province":"Iowa","country":"United States","zip":"50002","phone":null,"name":"test tes","province_code":"IA","country_code":"US","country_name":"United States","default":true}}}}
我的问题是,如何直接访问订单id(无循环)并检查订单id是否为null或空白


!

如果
response
是字符串,则可能需要对其进行解析,否则可以直接读取值

如果
console.log
显示了一个可以扩展的对象(在开发者工具控制台中),只需使用

如果这不起作用,或者你确定这是一个字符串使用

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

在执行
response.order.id
之前,需要确保将
response
解析为对象

提及

dataType : "json"
i、 e


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

var data = $.parseJSON(response);
alert(data.order.id); //you will get order id

response.order.id
?我尝试了-response.order.id
var obj=JSON.parse(response);console.log(obj.order.id)
可能是字符串?然后,
(JSON.parse(response)).order.id
@Jamiec-是!我使用var responseObj=JSON.parse(response);而且效果很好。