如何发送XMLHttpRequest Put调用(错误:重定向太多)

时间:2016-05-10 20:22:26

标签: javascript json shopify

我可以让GET电话很好,但我不知道如何发出PUT请求。我得到的错误是"无法加载资源:HTTP重定向太多"

代码:

var url = "https://{{shop.domain}}/cart.js";    
var xhrPut = new XMLHttpRequest();
xhrPut.open("PUT", url, true);
xhrPut.setRequestHeader("Content-Type", "application/json");
xhrPut.send("{\"total_price\":50}");

我试图改变的JSON是:

{"token":"a97c89f7e2c7c22becab8cfef40fa272","note":null,"attributes":{},"original_total_price":100,"total_price":100,"total_discount":0,"total_weight":0,"item_count":1,"items":[{"id":19673785606,"properties":null,"quantity":1,"variant_id":19673785606,"key":"19673785606:59d774c1f413525441dcb716e0c982e2","title":"Short Sleeve Shirt","price":100,"line_price":100,"original_line_price":100,"total_discount":0,"discounts":[],"sku":"","grams":0,"vendor":"Dev Store","product_id":6213990086,"gift_card":false,"url":"\/products\/short-sleeve-shirt?variant=19673785606","image":"https:\/\/cdn.shopify.com\/s\/files\/1\/1278\/7069\/products\/happyman.jpg?v=1462811058","handle":"short-sleeve-shirt","requires_shipping":true,"product_type":"","product_title":"Short Sleeve Shirt","product_description":"description","variant_title":null,"variant_options":["Default Title"]}],"requires_shipping":true}

total_price变量目前是100,我试图将其设置为50。

编辑这个以便我可以得到答案

我将代码更改为以下内容,然后收到错误400:错误请求

var url = "https://{{shop.domain}}/cart.json";
var xhrPut = new XMLHttpRequest();
xhrPut.open("PUT", url, false);
xhrPut.setRequestHeader("Content-Type", "application/json");
xhrPut.send('{"token":"a97c89f7e2c7c22becab8cfef40fa272","note":null,"attributes":{},"original_total_price":100,"total_price":50,"total_discount":0,"total_weight":0,"item_count":1,"items":[{"id":19673785606,"properties":null,"quantity":1,"variant_id":19673785606,"key":"19673785606:59d774c1f413525441dcb716e0c982e2","title":"Short Sleeve Shirt","price":100,"line_price":100,"original_line_price":100,"total_discount":0,"discounts":[],"sku":"","grams":0,"vendor":"Dev Store","product_id":6213990086,"gift_card":false,"url":"\\/products\\/short-sleeve-shirt?variant=19673785606","image":"https:\\/\\/cdn.shopify.com\\/s\\/files\\/1\\/1278\\/7069\\/products\\/happyman.jpg?v=1462811058","handle":"short-sleeve-shirt","requires_shipping":true,"product_type":"","product_title":"Short Sleeve Shirt","product_description":"description","variant_title":null,"variant_options":["Default Title"]}],"requires_shipping":true}"');

根据我对PUT请求如何工作的了解,这应该覆盖整个文档,但唯一的更改是:

  • 变量total_cost从100减少到50

  • 逃脱角色' \'是在角色之前使用的' \'在变量' url'在数组'项目'

这是我在此PUT请求之前发送的GET请求,它不返回任何错误并将预期值写入控制台。请注意,无论是运行还是注释掉,都会发生PUT请求的错误。

var totalCost;
var url = "https://{{shop.domain}}/cart.json";
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {           
  if (xhr.readyState == 4 && xhr.status == 200) {               
    var resp = xhr.responseText;
    console.log(resp);
    var json = JSON.parse(resp);
    console.log(json);                          
    totalCost = json.total_price;               
    console.log(totalCost);                                 
  }             
};          
xhr.open("GET", url, false);
xhr.send();
console.log(totalCost);

这是同步的唯一原因是我可以在API调用后记录totalCost。

0 个答案:

没有答案