当我通过表单的action属性向APi发出APi请求时,它工作正常,但是当我通过ajax发出相同请求时,出现跨域读取阻止(Access-control-allow-origin)错误浏览器:
这是我在ajax中的代码:
function post_to_form(form, fields) {
type: 'POST',
url: api_url,
data: formDataJSON,
complete: function( response ) {
if(response.status == 0 && response.statusText == "success"){
window.location = "https://preview.mysite.com/thank-you/";
}else{
console.log(JSON.stringify(response));
}
});
以下是我提交此表单的方式:
$('form').on('submit', function (e) {
e.stopPropagation();
e.preventDefault();
// if validation is ok, we can proceed with the form submition
var $form = $(this);
// Serializing data for Ajax request
var fields = $(this).serializeArray();
post_to_form($form,fields);
});
================
但是,如果我必须通过如下所示的表单操作属性提交表单,则提交工作没有错误。这是什么问题?
<form action="api_url" method="POST"