所以我试图用ajax发布,它只是不会通过。
这是我的javascript
$.ajax({
type: 'POST',
contentType: "",
url: 'some url',
data: order,
dataType: 'json',
success: function() {
Materialize.toast('Order added successfully', 4000, 'android-success');
console.log('order posted');
},
error: function() {
Materialize.toast('Something went wrong', 4000, 'android-error');
}
}); // $.ajax post
我没有收到错误,也没有从ajax获得成功。但我收到XHR failed loading: POST
我尝试过设置内容类型和数据类型无济于事。我的猜测,问题是对象格式或类似的东西。
可能有用的信息 - 这是我的代码的基本轮廓。
这是失败日志下的信息
send @ jquery-3.1.0.js:9392
ajax @ jquery-3.1.0.js:8999
(anonymous function) @ ajax.js:20
mightThrow @ jquery-3.1.0.js:3508
process @ jquery-3.1.0.js:3576
对此事的任何帮助都非常感谢。
答案 0 :(得分:5)
我得到了它的工作。问题在于页面重新加载。所以在post请求之前可以通过网页重新加载,这导致没有错误,只是一个失败的日志。呃,这样的菜鸟错误。
答案 1 :(得分:0)
我知道旧的问题,但如果它出现在Google搜索中,则仍然有意义。
在加载窗口时,为for添加一个事件侦听器,并阻止默认操作。
<script>
window.addEventListener("load", function () {
//not jquery!
// Access the form element...
var form = document.getElementById("myFormId");
// ...and take over its submit event.
form.addEventListener("submit", function (event) {
event.preventDefault(); // prevent form submission and reloading the page.
//your code to validate or do what you need with the form.
});
});
</script>