Hello.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Test JSONP</title> <script type="text/javascript" language="javascript"> var callbacks=new Object(); function getJSONP(url,cb) { var i; do i='c'+Math.floor(Math.random() * 99999); while(callbacks[i]); callbacks[i] = function(obj) { cb(obj); delete callbacks[i]; }; var script=document.createElement('script'); script.src=url+(url.indexOf('?')>=0?'&':'?')+'callback=callbacks.'+i; script.type='text/javascript'; document.body.appendChild(script); } </script> </head> <body> <script type="text/javascript"> // Callback function onSuccess(user) { alert(user); } </script> <input type="button" value="Получить JSONP" onclick="getJSONP('http://vkontakte.ru/feed2.php',onSuccess)"/> <div id="result"></div> </body> </html> 

This code for some reason writes an error Uncaught Syntax Error: Unexpected token : What can be wrong?

  • What? All Dunno? - Michael Nikolaev
  • With such visits you would be the reference bad manager. - yozh

1 answer 1

the result that is returned when executing http://vkontakte.ru/feed2.php should be in JS format, in this case instead of

 {"user":{"id":7594127},"friends":{"count":0},"messages":{"count":0},"events":{"count":0},"groups":{"count":0},"photos":{"count":0},"videos":{"count":0},"notes":{"count":0},"gifts":{"count":0},"lang":{"id":"0","p_id":0},"activity":{"hash":"d41d8cd98f00b204e9800998ecf8427e"}} 

should be displayed

 document.write(' {"user":{"id":7594127},"friends":{"count":0},"messages":{"count":0},"events":{"count":0},"groups":{"count":0},"photos":{"count":0},"videos":{"count":0},"notes":{"count":0},"gifts":{"count":0},"lang":{"id":"0","p_id":0},"activity":{"hash":"d41d8cd98f00b204e9800998ecf8427e"}} ')