So here is the scenario:
I am working with ASP.NET MVC 2 under the 4.0 framework and have a page that calls the $.load on an Action link. This is all fine, it works as expected. The page that is loading has a call to a javascript file:
<script type="text/javascript" src="somejs.js"/>
This is failing and saying that “Method Not Allowed” as it is trying to POST this call instead of GET. I can see this happening with firebug.
I have another js file in the project that is included on all pages with a call to:
$.ajaxSetup({
type: "POST",
cache: false,
timeout: 60 * 1000
});
this is so that I don’t have to make all of my $.ajax calls explicitly POST. If I change this to GET the $.load call works great. However, most of the rest of the site does not as Microsoft retired the GET method from MCV 2 or the 4.0 framework (I don’t really remember which).
I would rather not change all the $.ajax calls to POST explicitly, but I’m not sure what else to do at this point, so I was hoping to find an answer here.
Thanks.
Don’t inject HTML with
<script>tags into the document usingload()orhtml(). It’s highly unreliable cross-browser (and in jQuery, which may remove script tags completely depending on the situation), and even when it works it can cause you real confusion by loading the same script twice, potentially giving you duplicate definitions and handlers. (And of course, any script that relies on being executed at page load time is doomed.)If some script needs to be executed after a page load, either keep that code in static script and execute it in the
ajax/loadcallback function, or have the server return a JSON object containing the code in a string separately from the HTML.If you really must try to load
<script>tags dynamically you would have to… either that or change the call toajax()in jQuery-1.4.3.js line 4978 to include an explicittype: 'GET'setting. Arguably it’s a jQuery bug that this isn’t included. But on the other hand the whole premise of AJAX-loading<script>is flawed.