"Uncaught SyntaxError: Unexpected token:" in jsonp

I already got the json response data from server to browser, but it is confused that the data cannot be displayed in the browser and I ran into an error in the console that told me "Uncaught SyntaxError: Unexpected token:". Here is my code in node js.

function callFacebook(){
$.ajax({
    url: "http://192.168.37.179:8888/facebook/connect?callback_=[TIMESTAMP]",
        type: "GET",
        dataType: "jsonp",
        jsonp:"jsonp",
    cache: true,
    timeout: 5000,
    success: function(data) {
        execute(data);
    },
    error:function() { console.log('Uh Oh!'); }
});

      

} and here is the response json data: res.header('Content-Type','application/json'); res.header('Charset','utf-8'); res.send({"something": "father"});

0


source to share


1 answer


From the server you only send regular JSON data, but on the client you expect JSONP. The response from the server is not JSONP and the browser throws an exception because the syntax is invalid.

If you need to send JSONP from the server, then if you use express add extra use

:

app.configure(function() {
  app.set('jsonp callback', true);
});

      

Then just change slightly to send JSONP res

. You don't need to set the header anymore as they will be automatically detected:



res.jsonp({ hello: 'world' });

      

On the client side, jQuery will add the callback itself, so only use this simplified way:

$.ajax({
  url: "http://192.168.37.179:8888/facebook/connect",
  type: 'GET',
  dataType: 'jsonp',
  cache: true,
  timeout: 5000,
  success: function(data) {
    console.log(data)
  },
  error: function(xhr, status, error) {
    console.log('error[' + status + '] jsonp');
  }
});

      

Also, if your node is proxied through nginx or any other web framework, don't forget to include there utf8

.

0


source







All Articles