0

我正在尝试在 Node JS 中发出 PUT 请求以更新“值”字段。这是我的代码:

var identityJson = {};
identityJson.identity = {};
identityJson.identity.value = "+61111111111";
var url = "https://[Zendeskdomain].zendesk.com/api/v2/users/[user ID]/identities/[Identity ID].json";
var request = new XMLHttpRequest();

    request.onreadystatechange = function() {
        if (this.readyState==4 )
        {
            console.log(this.status)
            console.log(this.responseText);
        }
    }

request.open('PUT', url, true);
request.setRequestHeader("Authorization", "Bearer " + token);
request.setRequestHeader('Content-type', 'application/json');
request.send(JSON.stringify(identityJson));

此脚本返回 200 作为状态并像以前一样打印身份。在这种情况下,该值仍然是“+37011111111”

200
{"identity":{"url":"https://[Zendeskdomain].zendesk.com/api/v2/users/[user ID]/identities/[Identity ID].json","id":[Identity ID],"user_id":[user ID],"type":"phone_number","value":"+37011111111","verified":true,"primary":true,"created_at":"2018-09-07T12:24:02Z","updated_at":"2018-09-07T12:24:02Z"}}

如果我在客户端发送相同的请求(将脚本放在一个简单的网页上),该值确实会发生变化,因此输入的信息似乎格式正确。

有人在从 Node JS 提交 PUT 请求时遇到过类似的问题(无论是否在 Zendesk 中),并且可以指出我正确的方向吗?

注意: [Zendeskdomain]、[user ID] & [Identity ID] 与此无关,因此我删除了它们,但它们在脚本中正确输入。

谢谢!

4

1 回答 1

1

You're in server side, don't use XMLHttpRequest, it's a javascript client side function...

Use a package like request : https://www.npmjs.com/package/request

Then just do :

request({
  method: "PUT",
  uri: "http://sdfsfdsdf",
  json: body
 }, function(err, response, body){
    ....
});
于 2018-09-17T15:16:49.583 回答