jquery 如何将curl命令转换为ajax请求

bwleehnv  于 2023-06-22  发布在  jQuery
关注(0)|答案(2)|浏览(179)

我有命令到服务器获取信息

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"  http://test.kenhgiaiphap.vn/kprice/account/profile/get/token

我写 AJAX 来处理这个问题

$.ajax({
            url: "http://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 http://test.kenhgiaiphap.vn/kprice/account/profile/get/token. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

并且在请求中

token

test.kenhgiaiphap.vn/kprice/account/profile/get
OPTIONS
(canceled)
Load cancelled
text/plain
jquery-1.7.2.min.js:2320
Script
156B
0B
1.15s
39ms
39ms1.11s
slwdgvem

slwdgvem1#

这是浏览器问题。
dataType更改为jsonp或将callback=?添加到您的URL:

http://test.kenhgiaiphap.vn/kprice/account/profile/get/token?callback=?
hmae6n7t

hmae6n7t2#

您不能在客户端站点“同源策略问题”中使用发布。
你可以使用jsonp代替'json',并更改为get,基本上遵循“Gabriel桑托斯”的建议

相关问题