0

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
halfer
  • 19,824
  • 17
  • 99
  • 186
giaosudau
  • 2,211
  • 6
  • 33
  • 64

2 Answers2

1

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

Community
  • 1
  • 1
Gabriel Santos
  • 4,934
  • 2
  • 43
  • 74
  • In curl command I have result: {"status":"success","account":{"buy_title":"KGP ID","birthday":0,"sex":0,"phone_number":"","avatar":"http:\/\/images.kenhgiaiphap.vn\/test_images\/1349882062814\/avatar\/avatar.png","password":"","virtual_id":"1349882062814","point":0,"quota":2,"level":1,"remain":22421389413,"token":"a5a95c30274611e2ae10000c29bb7331","email":"hovanke@gmail.com","buy_link":"http:\/\/id.kgp.vn","fullname":"hovanke"}}* but in ajax I have try {"status":"failure","msg":"0107"} – giaosudau Nov 06 '12 at 03:06
  • It has error Uncaught SyntaxError: Unexpected token : test.kenhgiaiphap.vn/kprice/account/profile/get/token?callback=jQuery172009154598484747112_1352171086297&_=1352171101691:1 Uncaught SyntaxError: Unexpected token : token:1 – giaosudau Nov 06 '12 at 03:11
  • Jsonp will not work because the response server does not support it. – Gabriel Santos Nov 06 '12 at 03:13
  • So, you need to use curl or a server approach. – Gabriel Santos Nov 06 '12 at 03:13
  • The callback is like `JSONP`, which will not work too. As I have said, Ajax is not the best option. – Gabriel Santos Nov 06 '12 at 03:16
  • what is best solution for this? – giaosudau Nov 06 '12 at 03:22
  • Avoid AJX and continue with CURL. – Gabriel Santos Nov 06 '12 at 03:23
1

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

AlexC
  • 9,657
  • 17
  • 64
  • 98