【发布时间】:2017-10-16 17:43:41
【问题描述】:
我正在尝试将表单数据发布到我的服务器。我已经编写了以下 ajax 调用,但我不断收到 400 Bad 错误。有什么帮助吗?
$(document).ready(function(){
// click on button submit
$("#submit").on('click', function(){
// send ajax
$.ajax({
url: "/compare",
type : "POST",
contentType : 'application/json; charset=utf-8',
data : $('#form').serialize(),
success : function(result) {
console.log(result);
},
error: function(xhr, resp, text) {
console.log(xhr, resp, text);
}
})
});
});
以下是我的 HTML 表单:
<form id="form">
<p>Input the URL of 2 images!</p>
<input id="img1" name="img1" type="text" placeholder="Image 1 URL">
<input id="img2" name="img2" type="text" placeholder="Image 2 URL">
<input id="submit" type="submit" value="Compare!">
</form>
【问题讨论】:
-
您在
/compare的服务器是否提供HTTPPOST方法? -
@davidbuzatto 是的。但它期待一个 JSON
-
您正在序列化一个表单,但您将它作为 JSON 发送。我们不知道服务器期望什么,但这样做会得到奇怪的数据。
-
@adeneo 我正在尝试将表单数据作为 json 发布。服务器期望数据为 Json。
-
我基本上希望服务器收到:{ 'img1': URL_HERE, 'img2': URL_HERE }
标签: javascript html ajax