Note that there are some explanatory texts on larger screens.

plurals
  1. POjsonp and REST GET call
    text
    copied!<p>I have two projects, an older one, which works, and a newer one. Both projects have to make a cross-domain call.</p> <p>On the older project, I use jsonp to talk to the server and get a response, such as:</p> <pre><code>var myUrl = "http://www.myserver.whatever/somepage.php"; jsonp(myUrl, "ajaxResponse"); function ajaxResponse(data) { alert(data.response); } </code></pre> <p>In my new totally unrelated project, I have to talk to a service. The web page is a local file on the client talking to a CentOS daemon written by a software vendor. I can communicate to the daemon through REST GET calls. An example of a call would be, which works, as I entered the following in a Mozilla Firefox browser window:</p> <p>localhost:8080/mfds/info</p> <p>I thought that the easiest way to communicate to the daemon would be through jsonp, like I did previously, so:</p> <pre><code>var myUrl = "http://localhost:8080/mfds/info"; jsonp(myUrl, "ajaxResponse2"); function ajaxResponse2(data) { alert(data.response); } </code></pre> <p>Sadly, I never get to the ajaxResponse2() function. What am I doing wrong? Is jsonp() not a REST GET call? How do I fix the code?</p> <p>I am using:</p> <pre><code>&lt;script type="text/javascript" src="js/jsonpCrossDomain.js"&gt;&lt;/script&gt; </code></pre> <p>to load the jsonp script. As mentioned earlier, if I use the URL associated with my old project, the code works, just not with localhost:8080:/mfds/info, which works nicely in a browser. The daemon service returns html code.</p> <p>UPDATE: Based on the comment below, I did not realize that jsonp has to be on both sides. I originally tried the following code, but this code threw a cross domain error (or so I think, actual error below), which led me to try the JSONP call, but I cannot use that either.</p> <pre><code>$.ajax({ url: 'localhost:8080/mfds/info', type: "GET", dataType: "html", error: function (error) { alert(JSON.stringify(error)); }, complete: function (data_response) { alert(data_response.responseText); }, success: function (data) { alert(data); } }); </code></pre> <p>Error message from the $.ajax call:</p> <p>{"readyState":0,"status":0,"statusText":"[Exception... \"\" nsresult: \"0x805e0006 ()\" location:\"js frame ::file:///home/user/Documents/myproejct/js/jquery-2.0.3.min.js :: :: line 6\" data: no]"}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload