jQuery:从json响应中获取订单ID

Nil*_*ara -1 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";
})
Run Code Online (Sandbox Code Playgroud)

我从服务器端脚本得到如下响应:

{"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}}}}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何直接访问订单ID(无循环)并检查订单ID是否为null或空白。

Jam*_*iec 5

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

如果console.log显示可以扩展的对象(在开发人员工具控制台中),请使用

var id = response.order.id;
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,或者您确定它是字符串使用

var responseObj = JSON.parse(response);
var id = responseObj.order.id;
Run Code Online (Sandbox Code Playgroud)