Note that there are some explanatory texts on larger screens.

plurals
  1. POJSONP and DOCTYPE Errors
    primarykey
    data
    text
    <p>I'm running into a weird problem.</p> <p>I'm communicating with my server using AJAX. (I'm running my web application on <code>localhost</code>). Server is located on, say, <code>http://www.example.com</code></p> <p>To bypass the Same Origin Policy, I'm using JSONP. I dynamically create a <code>&lt;script&gt;</code> tag and load the data from my server.</p> <p>So far so good.</p> <p>Then I decided to upload my web application to this subdomain: <code>http://m.example.com</code></p> <p>That's when I run into crazy errors. Sometimes the page loads, sometimes it doesn't. When it doesn't load, Firebug throws a <code>DOCTYPE</code> error.</p> <p>I did some research and came across this stackoverflow post: <a href="https://stackoverflow.com/questions/7714896/firebug-returns-syntax-error-in-doctype">firebug returns syntax error in doctype?</a></p> <p><strong>Quoting an answer in this link:</strong></p> <blockquote> <p>This usually happens because you are loading an HTML document as a script. This is often caused by <code>&lt;script src=""&gt;&lt;/script&gt;</code> (i.e. a relative URI pointing at the current, HTML, document)) or one of the scripts pointing to a 404 error.</p> </blockquote> <p>Pretty helpful stuff. Based on all that, I've concluded from all the above that whenever my server responds slowly, the <code>&lt;script&gt;</code> tag's <code>src</code> attribute is null. Since that throws a <code>404 error</code>, I get a <code>DOCTYPE</code> error in Firebug. Whenever my server responds quickly, there are no issues and everything works fine.</p> <p>How do I solve this problem? I could put a manual timeout or something, but that wouldn't exactly be foolproof and an elegant solution.</p> <p>Any help guys?</p> <h1>EDIT:</h1> <p><strong>Here's some code:</strong></p> <p>This function is used to create the <code>script</code> tag dynamically:</p> <pre><code>function appendScriptToHead() { var element = document.createElement("script"); element.src = 'http://www.example.com/?data&amp;callback=callfunction'; document.getElementsByTagName("head")[0].appendChild(element) } </code></pre> <p>This callback function is called when the above url containing JSONP data is loaded: </p> <pre><code>function callfunction(response) { alert(response); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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