html5 - JSONP callback issue -


i'm trying jsonp working server running on arduino. js code:

window.onload = init;  function init() {     //alert("test");     sendrequest(); }  function sendrequest() {     alert("sending request");     var url = "http://192.168.1.177";     var request = new xmlhttprequest();     request.open("get", url);     request.error = function(e) {             alert("error");         };     request.send(null); }   function arduinojsonp() {     alert("callback received!!!"); } 

the callback function never reached.

but if direct browser directly arduino ip see following displayed in browser:

arduinojsonp({"data": 12345}) 

so seems server sending response correct jsonp format somehow function not invoked. there else need js call function? tried moving function html body didn't either.

thank you.

you not handling server response @ all. if doing without libraries need eval result returned server.

and jsonp implementation not xhr, have inject script tag html correct src attribute.

just use library have logic abstracted you.

simply inject script tag html tree:

function sendrequest() {     var element = document.createelement('script');      var s = document.getelementsbytagname('script')[0];     element.type = 'text/javascript';      element.async = true;     element.src = 'http://192.168.1.177';     s.parentnode.insertbefore(element, s); } 

you can mark unique id. hookup onload event , once executed remove script.


Comments