【问题标题】:How to convert string to ByteString in Ajax Post Request?如何在 Ajax 发布请求中将字符串转换为 ByteString?
【发布时间】:2018-02-03 02:04:21
【问题描述】:

我想使用 ajax 来检索令牌。

按照这个documentation,下面是URL、请求头和请求体

POST https://api-production.august.com/session

Request Headers:

x-august-api-key: 727dba56-fe45–498d-b4aa-293f96aae0e5
x-kease-api-key: 727dba56-fe45–498d-b4aa-293f96aae0e5
Content-Type: application/json
Accept-Version: 0.0.1
User-Agent: August/Luna-3.2.2

Request Body: (JSON Encoded)
{
  "installId": <Random UUID>,
  "password": "XXXXXXXX",
  "identifier": "phone:+15555551234"
}

我已经尝试使用 ajax 来实现这一点

  $.ajax({
            type: "POST",
            url: "https://api-production.august.com/session",
            dataType: "json",
            headers:{
                "x-august-api-key":"727dba56-fe45–498d-b4aa-293f96aae0e5",
                "x-kease-api-key":"727dba56-fe45–498d-b4aa-293f96aae0e5",
                "Content-Type": "application/json",
                "Accept-Version": "0.0.1",
                "User-Agent": "August/Luna-3.2.2"
            },
            data: JSON.stringify({installId: "7fb17963-21a8-4c23-8f81-121ed3298ad8",
            password: "password",
            identifier: "phone:+18885554444"})})

但是我没有得到令牌,我得到以下响应:

abort: function abort()
always: function always()
catch: function catch()
done: function add()
fail: function add()
getAllResponseHeaders: function getAllResponseHeaders()
getResponseHeader: function getResponseHeader()
overrideMimeType: function overrideMimeType()
pipe: function pipe()
progress: function add()
promise: function promise()
readyState: 0
responseJSON: undefined
setRequestHeader: function setRequestHeader()
state: function state()
status: 0
statusCode: function statusCode()
statusText: "error"
then: function then()

错误如下:

my error: ajax TypeError: Cannot convert string to ByteString because the character at index 13 has value 8211 which is greater than 255.

我也试过在谷歌浏览器中运行它,但我收到以下错误:

"TypeError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': Value is not a valid ByteString."

我也尝试了以下方法,但没有成功,因为我得到了同样的错误:

   $.ajax({
                type: "POST",
                url: "https://api-production.august.com/session",
                dataType: "json",
                  beforeSend: function(request) {
                    request.setRequestHeader("x-august-api-key", "727dba56-fe45–498d-b4aa-293f96aae0e5");
                  },
                data: JSON.stringify({installId: "7fb17963-21a8-4c23-8f81-121ed3298ad8",
                password: "password",
                identifier: "phone:+18885554444"})})

【问题讨论】:

标签: ajax post header xmlhttprequest


【解决方案1】:

您的第一个错误有点含糊,但确实提到了“索引 13 处的字符”(又名第 14 个字符)。您的第二个错误帮助您看到问题来自setRequestHeader。因此,您应该查看传递给 setRequestHeader 的字符串,更具体地说,是这些字符串中的第 14 个字符。

  1. x-august-api-key
  2. 727dba56-fe45–498d-b4aa-293f96aae0e5

现在我们问自己,k 或 – 中的哪一个字符更有可能给我们带来问题?应该很直观,– 更有可能是冒犯的角色,所以我们仔细研究一下。

此时我们应该怀疑我们使用的不是连字符(ascii 值为 45),而是其他一些看起来像连字符的字符。事实上,如果我们查找 ascii 值 8211(8211 来自您的第一个错误),我们会看到关联的字符是破折号。

一些编辑器/查看器使字符看起来不同,而另一些则使其看起来相同。对于我使用谷歌浏览器来查看这个问题,我可以比较你的键中的连字符,我可以看到第二个“连字符”看起来不像其他的。有点长。

hyphen:  -
en dash: –

因此,您应该将短划线字符替换为键中的连字符。那么你的字符串应该可以转成ByteString没问题了。

【讨论】:

  • 破折号不是我的问题,但指向位置 n 的字符的错误是,所以我看了看,我有一个看起来像三个句点的字符... 在我复制粘贴的密钥中,但是是单个字符。我从我使用的 Bearer 密钥中删除了它,并且错误消失了(在本例中来自 Swagger)。 +1
  • 在我之前的评论中,看起来这是 Firefox 中的一个编码错误,我切换到新的 Edge(V.94) 并且省略号字符没有同样的问题。虽然删除“错误”字符消除了 ByteString 错误,但我无法进行身份验证,现在有一个错误的令牌。
猜你喜欢
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 2017-02-07
  • 2022-01-08
  • 1970-01-01
相关资源
最近更新 更多