javascript - Ajax: rest api call give error: Uncaught SyntaxError: Unexpected token : -
i trying understand problem code work rest api
i using "espocrm" , want start working api.
in documentation ask use: uses basic authentication like:
"authorization: basic " + base64encode(username + ':' + password)
so try use code:
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script> <script type="text/javascript" > var creds = { username: "myuser", password: "mypass" }; var credentials = btoa(creds.username + ":" + creds.password); $.ajaxsetup({ xhrfields: { withcredentials: false }, beforesend: function (xhr) { xhr.setrequestheader("authorization", "basic" + credentials); return true; } }); $.ajax({ url: 'http://crmurl.com/api/v1/app/user', type: 'get', datatype: 'jsonp', async: false, success: function (data) { console.log(data); var json = json.parse(data); alert(json.user.username); } }); </script>
after use code error in console:
uncaught syntaxerror: unexpected token :
when click on error link can see json data. because of error can't work data. no matter try.
if change datatype: 'jsonp'
datatype: 'json'
i error:
xmlhttprequest cannot load http://crmurl.com/api/v1/app/user. no 'access-control-allow-origin' header present on requested resource. origin 'http://www.domain.com' therefore not allowed access. response had http status code 401.
i have add in htaccess
<ifmodule mod_headers.c> header set access-control-allow-origin: * </ifmodule>
the json output is:
{"user":{"id":"1","name":"admin","deleted":false,"isadmin":true,"username":"admin","password":"xna3ppcgycigqje4gzi4gnej1tv9xf1m7f490qtg.ylpg3y3qtwrwqq.4ricyiro8akeozxiwnxzukg4p4jnx1","salutationname":"","firstname":"","lastname":"admin","isactive":true,"title":"","emailaddress":"demo@espocrm.com","phonenumber":"+44(203)695-03-80","createdat":"2015-07-11 05:03:05","defaultteamid":null,"defaultteamname":null,"teamsids":[],"teamsnames":{},"avatarname":null,"avatarid":null},"acl":{"table":{"email":{"read":"all","edit":"all","delete":"no"},"emailaccountscope":true,"emailtemplate":{"read":"all","edit":"all","delete":"no"},"account":{"read":"all","edit":"all","delete":"no"},"calendar":true,"call":{"read":"all","edit":"all","delete":"no"},"campaign":{"read":"all","edit":"all","delete":"no"},"case":{"read":"all","edit":"all","delete":"no"},"contact":{"read":"all","edit":"all","delete":"no"},"document":{"read":"all","edit":"all","delete":"no"},"documentfolder":{"read":"all","edit":"all","delete":"no"},"lead":{"read":"all","edit":"all","delete":"no"},"meeting":{"read":"all","edit":"all","delete":"no"},"opportunity":{"read":"all","edit":"all","delete":"no"},"targetlist":{"read":"all","edit":"all","delete":"no"},"task":{"read":"all","edit":"all","delete":"no"},"user":{"read":"all","edit":"no","delete":"no"},"team":{"read":"all","edit":"no","delete":"no"},"note":{"read":"all","edit":"own","delete":"own"},"emailaddress":{"read":"no","edit":"no","delete":"no"},"phonenumber":{"read":"no","edit":"no","delete":"no"},"emailaccount":{"read":"own","edit":"own","delete":"own"},"role":false},"assignmentpermission":"all","userpermission":"no"},"preferences":{"id":"1","timezone":"utc","dateformat":"mm\/dd\/yyyy","timeformat":"hh:mm","weekstart":0,"thousandseparator":",","decimalmark":".","defaultcurrency":"usd","dashboardlayout":[{"name":"my espo","layout":[[{"name":"stream","id":"d4"},{"name":"salesbymonth","id":"d11"},{"name":"salespipeline","id":"d12"}],[{"name":"tasks","id":"d3"},{"name":"opportunitiesbyleadsource","id":"d14"},{"name":"opportunitiesbystage","id":"d15"}]]}],"dashletoptions":null,"smtpserver":"","smtpport":25,"smtpauth":false,"smtpsecurity":"","language":"es_es","exportdelimiter":";","receiveassignmentemailnotifications":true,"autofollowentitytypelist":[],"signature":"<br>","defaultreminders":[]},"token":null}
when make call json access denied cors headers absent. hence error
xmlhttprequest cannot load http://crmurl.com/api/v1/app/user. no 'access-control-allow-origin' header present on requested resource. origin 'http://www.domain.com' therefore not allowed access. response had http status code 401.
this explains second error. now, since cors not present, jsonp way data, adds cors headers.
the data ajax callback json itself. cannot parse json data parse
returns json data itself. following code unnecessary
json.parse(data);
just assign
var json = data;
or use data
directly. solve first error.
Comments
Post a Comment