-1

I want to get the value of the json response. I wrote an Ajax function as below:

$.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"; }) 

I get the response from the server side script as below:

{"order": {"id":303657910281, "email":"[email protected]", "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":"[email protected]","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":"[email protected]","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}}}} 

My question is, how to access the order id directly (no loop) and check if the order id is not null or blank.

!

4
  • 5
    response.order.id ? Commented Mar 19, 2018 at 8:17
  • I tried - response.order.id Commented Mar 19, 2018 at 8:17
  • var obj = JSON.parse(response); console.log(obj.order.id) Commented Mar 19, 2018 at 8:19
  • 1
    Maybe it's a string? Then, (JSON.parse(response)).order.id Commented Mar 19, 2018 at 8:19

3 Answers 3

5

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; 
Sign up to request clarification or add additional context in comments.

1 Comment

@Jamiec- Yes! I used var responseObj = JSON.parse(response); and it works well.
4

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

Mention

dataType : "json" 

i.e.

$.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"; }) 

Comments

0

In your AJAX success you need to decode the JSON.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.