skip to Main Content

I’m trying to create an order using JS. I’ve authenticated my app and have a function that POST’s to orders.json. I see a status code of 200, indicating that the request submitted OK (right?) but the order itself never gets created. I’ve heard something about disabling cookies in my request, but I don’t know how to do that, so if that’s what I need to do please let me know.

I’m putting up my entire function, since I’m new to this entire thing and it seems that it’s probably the structure of my request, not the API call. I’m seeing the “Error” log in the console, so clearly the error function is running.

function submitShuffle(prodid)
{
  console.log(prodid);
  var params = {
    "order": {
        "line_items": [
        {
            "variant_id": prodid,
            "quantity": 1
        }
      ]
    }
    };
    $.ajax({
      type: 'POST',
      url: 'https://<my-usrnam>:<my-pass>@toyboxshufflesandbox.myshopify.com/admin/orders.json',
      dataType: 'application/json',
      data: params,
      success: function(data){
        console.log(data);
      },
      error: function(data){
        console.log("Error");
        console.log(data);}
    });
}

2

Answers


  1. Shopify returns an Object when you make a call. Hence 200 OK status. Inspect the object returned. A failed create POST object will not have an ID. So there is clue number one. Secondly, you’ll see that Shopify tells you what the problem was in the Error key of the returned object. If you cannot figure out what you did with the message from Shopify, make the same call to the GraphQL endpoint if you can, as the error messages Shopify returns from those endpoints are currently much better. They are backporting them to the older REST API, but for now, GraphQL is more expressive.

    Login or Signup to reply.
  2. You cannot retrieve information from Shopify Admin API by AJAX. There is a limitation about this because you have to expose your username/key and password which is not a good idea.

    You have to use some service/app or just to create a custom app.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search