2

I have created a simple form to return a list of translation keys through the API. But it says Uncaught SyntaxError: Unexpected token : Here is my sample code.

My form:

<form id="upload_form">
    <button type="submit">Ajax Request</button>
</form>

My ajax call:

$("#upload_form").submit(function(e){
e.preventDefault();
$.ajax({
    url:'https://lokali.se/api/string/list',
 data:'api_token=641bc8455768b09589806c7d489640789cfb49e1&id=7509115157679787966292.60209031',
    dataType:'jsonp',
    type:'post',
    contentType: 'application/x-www-form-urlencoded',

    success:function(response){
    console.log('hi');
    console.log(response);      
    },
});    
});

When I tested the API through postman it returns the Success OK 200 response For reference please find the screenshot Postman

My error is from here {"response":{"status":"error","code":4049,"message":"Missing request parameter"}} Can anyone help me out here?

saravana
  • 311
  • 4
  • 14
  • could you please try like : instead of `data:api_token=641bc8455768b09`; try passing an object like `data: {api_token : '641bc84557...', id: '7509115...'}` – vijayP Jun 28 '16 at 13:35
  • 1
    its because you use jsonp, why not just use json?, heres the same question http://stackoverflow.com/questions/10507345/ajax-and-jsonp-parseerror-and-uncaught-syntaxerror-unexpected-token – inubs Jun 28 '16 at 13:40
  • @vijayP `{api_token : '64...', id: '7509..'}` still same error :( – saravana Jun 28 '16 at 13:40
  • @MaanusIndov If I use json I am getting the cross domain error `No 'Access-Control-Allow-Origin' header is present on the requested resource` – saravana Jun 28 '16 at 13:47
  • witch language you use to write backend API? you can allways allow the origins, so no 3rd party application could not do requests **PHP:** `header("Access-Control-Allow-Origin: *");` * allows all, replace the * with your domain for more secure approach – inubs Jun 28 '16 at 13:50

1 Answers1

0

Can you try like the following ?

$("#upload_form").submit(function(e){
e.preventDefault();
  var envelope = {};
  envelope.api_token = '641bc8455768b09589806c7d489640789cfb49e1';
  envelope.id='7509115157679787966292.60209031';
$.ajax({
    url:'https://lokali.se/api/string/list',
    data:envelope,
    dataType:'json',
    type:'post',
    contentType: 'application/x-www-form-urlencoded',
    success:function(response){
    console.log('hi');
    console.log(response);      
    },
});    
});
Ozan Gunceler
  • 1,067
  • 11
  • 20