[jQuery] Q: on handling GET JSON
<span style="border-collapse:collapse"><div>I'm confused by something I found in the definition of $.ajax in the jQuery source, and would greatly appreciate it if someone could shed some light on the matter. The part of the code in question is this (I've made a couple of minor formatting changes for clarity):
</div><div>
</div><blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="font-size:12px">// If we're requesting a remote document</span></blockquote><blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="font-size:12px">// and trying to load JSON or Script with GET</span></blockquote><blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="font-size:12px">if ( (!s.url.indexOf("http") || !s.url.indexOf("//")) &&</span></blockquote><blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="font-size:12px"> ( s.dataType == "script" || s.dataType =="json" ) &&</span></blockquote><blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="font-size:12px"> s.type.toLowerCase() == "get" ) {</span></blockquote><blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="font-size:12px">
</span><span style="font-size:12px"> var head = document.getElementsByTagName("head")[0];</span>
<span style="font-size:12px"> var script = document.createElement("script");
</span>
<span style="font-size:12px"> /*</span>
<span style="font-size:12px"> * definition of script omitted</span>
<span style="font-size:12px"> */</span>
<span style="font-size:12px">
head.appendChild(script);</span></blockquote><blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="font-size:12px">
</span></blockquote><blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-width:initial;border-color:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px">
<span style="font-size:12px"> // We handle everything using the script element injection
</span><span style="font-size:12px"> return undefined;</span>
<span style="font-size:12px">}</span>
</blockquote>
<div>
</div><div>I understand what the test at the top is testing for, but I don't understand the policy implemented by the code that runs when the test evaluates to true.</div><div>
</div><div>Couldn't a script be requesting simple JSON data (
e.g. the JSON string '[ "foo" ]') via GET? I think the code shown above would cause such a script to fail.</div><div>
</div><div>This code suggests to me that jQuery expects such data to come only from relative URLs, but I don't understand what justifies this expectation.
</div><div>
</div><div>TIA!</div><div>
</div><font color="#888888"></font></span><font color="#888888"></font>