我正在尝试使用后端团队给我的curl使用jQuery AJAX进行API调用。
curl -X POST \
https://example1.com/api/sms \
-H 'Accept: application/json' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 's: APP' \
-d '{
"mobile": "1110002222"
}'
但是它抛出403禁止错误:
Invalid CORS request
这是另一个域名(example2.com)上托管的代码:
data = {mobile: '0001112222' };
success = function(data) { console.log(data); };
headers = {s: 'APP'};
$.ajax({
type: 'POST',
url: 'https://example1.com/api/sms',
data: data,
success: success,
dataType: 'json',
headers: headers
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我在做什么错还是想念?
答案 0 :(得分:1)
我已经阅读了您的问题,请尝试以下代码:
var settings = {
"async": true,
"url": "https://example1.com/api/sms",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"s": "APP",
"Cache-Control": "no-cache",
},
"processData": false,
"data": "{\"mobile\": \"1110002222\"}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
此请求:
curl -X POST \
https://example1.com/api/sms \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 's: APP' \
-d '{"mobile": "1110002222"}'