我有命令curl到服务器获取信息
curl -v -H "Content-Type:application/json" -H "X-KGP-AUTH-TOKEN: a5a95c30274611e2ae10000c29bb7331" -H "X-KGP-APPID:id.kenhgiaiphap.kcloud" -H "X-KGP-APPVER:0.0.1" -H "X-KGP-DEVID:xxx" -H "X-KGP-DEVTYPE:xxx" https://test.kenhgiaiphap.vn/kprice/account/profile/get/token
我写ajax来处理这个问题
$.ajax({ url: "https://test.kenhgiaiphap.vn/kprice/account/profile/get/token", type: "POST", cache: false, dataType: 'json', success: function() { alert('hello!'); }, error: function(html) { alert(html); }, beforeSend: setHeader }); function setHeader(xhr) { xhr.setRequestHeader('X-KGP-AUTH-TOKEN','a5a95c30274611e2ae10000c29bb7331'); xhr.setRequestHeader('X-KGP-APPVER', '0.0.1'); xhr.setRequestHeader('X-KGP-DEVID', 'xxx'); xhr.setRequestHeader('X-KGP-APPID','id.kenhgiaiphap.kcloud'); xhr.setRequestHeader('X-KGP-DEVTYPE', 'xxx'); }
但我有问题是
2XMLHttpRequest cannot load https://test.kenhgiaiphap.vn/kprice/account/profile/get/token. Origin https://localhost is not allowed by Access-Control-Allow-Origin.
并在请求中
token
test.kenhgiaiphap.vn/kprice/account/profile/get OPTIONS(已取消)加载取消的文本/普通jquery-1.7.2.min.js:2320脚本156B 0B 1.15s 39ms 39ms1.11s
感谢支持!
这是一个浏览器问题。
将dataType
更改为jsonp
或添加callback=?
到你的url:
https://test.kenhgiaiphap.vn/kprice/account/profile/get/token?callback=?
未来请参阅https://stackoverflow.com/a/6396653/744255
您不能在客户端站点“同源策略问题”中使用post。
Ou可以使用jsonp
代替’json’并改为获取,几乎遵循“ Gabriel Santos ”的建议
以上就是jQuery教程分享如何从curl命令转换为ajax请求相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/jquerytutorial/547280.html