【发布时间】:2017-11-12 15:00:03
【问题描述】:
我正在尝试使用以下代码向 url 发出 post 请求。 将对象传递给 http.send(params) 函数会给出 (400) 错误请求错误。 我无法在这里追踪问题。
var http = new XMLHttpRequest()
var url = 'http://somerandomurl'
http.open('POST', url, true)
http.setRequestHeader('content-type', 'application/json')
http.setRequestHeader('accept', 'application/json')
http.onreadystatechange = function () {
if (http.readyState === 4 && http.status === 200) {
returndata = http.responseText
console.log(JSON.parse(returndata))
}
}
http.send(params)
解决方案:http.send(JSON.stringify({'email': params.email, 'password': params.password})) 它对我有用。
【问题讨论】:
-
params 中的数据是什么?还要发布服务器端代码。您能否通过邮递员或休息客户端成功提出请求?如果是这样,则可能是上述代码的问题,否则可能是服务器端代码问题。
-
您确定您的
params对象是有效的 JSON 吗?你的代码对我有用。 -
@KrishnadasPC 这是一个完整的 json 对象,已传递给此函数。
-
@Mark_M 是的,Caleb 的解决方案对我有用。谢谢
-
很高兴它成功了。当您发布包含完整数据的问题帖子时。您可以使用虚拟值,但发布完整信息总是有帮助的。
标签: javascript http post vue.js