giaosudau
giaosudau

Reputation: 2251

How to transform from curl command to ajax request

I have command to curl to server to get information

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

I write ajax to handle this

 $.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');
        }

But I have problem is

2XMLHttpRequest cannot load http://test.kenhgiaiphap.vn/kprice/account/profile/get/token. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

and in request is

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

Upvotes: 0

Views: 3306

Answers (2)

AlexC
AlexC

Reputation: 9661

You cannot use post in client site "Same Origin Policy issue".

Ou can use jsonp instead 'json' and change to get, pretty much following "Gabriel Santos" suggestion

Upvotes: 1

Gabriel Santos
Gabriel Santos

Reputation: 4974

This is a browser issue.

Change dataType to jsonp or add callback=? to your url:

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

Future refer to https://stackoverflow.com/a/6396653/744255

Upvotes: 1

Related Questions