Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Easiest way to do that, is not to call a server through an ajax, but instead to create a new script tag on the page with the url pointing to a RESTful web-service that would output pure JavaScript (not JSON). That way your output will be evaluated by the browser directly without the use of eval.</p> <p>To expand a little on my answer: To get around the problems of running script in the global context you could do some tricks. For example, when you are adding script tag to the head, you can bind onload event (or rather fake onload event, since IE doesn't support onload on the script tag) to it, and if your response from the server will be always wrapped in the the function with a known name, you could apply that function from within your object. Example code below (this is just an example though):</p> <pre><code>function test () { this.init = function () { var script = document.createElement("script"); script.type = "text/javascript"; script.language = "javascript"; script.src = "test.js"; var me = this; window.callMe = function () { me.scriptReady(me); }; var head = document.getElementsByTagName("head")[0]; head.appendChild(script); }; this.scriptReady = function (object) { serverResponse.call(object); }; this.name = "From inside the object"; this.init(); } var t=new test(); </code></pre> <p>The server response should look something like this:</p> <pre><code>function serverResponse() { alert(this.name); } window.callMe(); </code></pre> <p>In this case, everything inside serverResponse() will use your object as "this". Now if you modify your server response in this way:</p> <pre><code>function serverResponse() { this.serverJSONString = { "testVar1": "1", "testVar2": 2 }; function Test() { alert("From the server"); } Test(); } window.callMe(); </code></pre> <p>You can have multiple things being returned from the server and with just one response. If you don't like just setting variables, then create a function in your main object to handle JSON string that you can supply by calling this function from your response.</p> <p>As you can see, it's all doable, it really doesn't look pretty, but then again, what you are trying to do is not pretty to begin with.</p> <p>P.S. Just inserting a string inside tag will not work for IE, it will not allow you to do that. If you don't have to support IE, then you could get away with just inserting server response inside a newly created script tag and be done with it.</p> <p>P.P.S. Please don't use this code as is, cause I didn't spend too much time writting it. It's ugly as hell, but was just ment as an example:-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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