【发布时间】: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"})})
【问题讨论】:
-
在我的情况下,这个答案的内容就是解决方案:stackoverflow.com/a/31439605。
标签: ajax post header xmlhttprequest