我有一个API,我想使用ajax发布值。但是我收到了Uncaught SyntaxError:Unexpected token,并且在测试时我无权访问。
<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
<script type="text/javascript">
function getFormData($form){
var unindexed_array = $form.serializeArray();
var indexed_array = {};
$.map(unindexed_array, function(n, i){
indexed_array[n['name']] = n['value'];
});
return indexed_array;
}
var $form = $("#postform");
var data = getFormData($form);
$.ajax({
url: 'https://example/run/t1/go',
headers: {'Access-Control-Allow-Origin: *','Access-Control-Allow-Credentials: true','X-Requested-With':'XMLHttpRequest','Content-Type':'application/x-www-form-urlencoded'
},
method: 'POST',
dataType: 'json',
data: data,
success: function(data){
console.log('success: '+data);
}
});
</script>