jQuery:从json响应中获取订单ID [重复]
Posted
技术标签:
【中文标题】jQuery:从json响应中获取订单ID [重复]【英文标题】:jQuery: get order id from json response [duplicate] 【发布时间】:2018-08-27 17:48:20 【问题描述】:我想获取 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
我的问题是,如何直接访问订单 ID(无循环)并检查订单 ID 是否为空或空白。
!
【问题讨论】:
response.order.id
?
我试过了 - response.order.id
var obj = JSON.parse(response); console.log(obj.order.id)
可能是字符串?然后,(JSON.parse(response)).order.id
【参考方案1】:
如果response
是字符串,可能需要解析,否则可以直接读取值。
如果console.log
显示一个您可以展开的对象(在开发者工具控制台中),只需使用
var id = response.order.id;
如果这不起作用,或者你确定它是一个字符串使用
var responseObj = JSON.parse(response);
var id = responseObj.order.id;
【讨论】:
@Jamiec- 是的!我使用了 var responseObj = JSON.parse(response);而且效果很好。【参考方案2】:在您的 AJAX 成功中,您需要解码 JSON。
var data = $.parseJSON(response);
alert(data.order.id); //you will get order id
【讨论】:
【参考方案3】:你需要确保response
在做response.order.id
之前被解析成一个对象
提及
dataType : "json"
即
$.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";
)
【讨论】:
以上是关于jQuery:从json响应中获取订单ID [重复]的主要内容,如果未能解决你的问题,请参考以下文章
Jquery Json Array 响应未在 Div 中打印
无法使用 jquery 从 Web API 获取 JSON 响应