JQuery $ .ajax () json calls the * .js file, makes the browser ask for "save / open",

I can issue $ .ajax () requests for URLs that have the .js extension just fine when I use {... 'dataType': 'script' ...}. However, when I use {... 'dataType': 'json' ...} the browser (Opera and FF, for now, but I'll bet it's generic) asks to save / open the query results.

Note that my "successful" callback works fine and uses the resulting json object just fine.

My question is, should I just remove the * .js extension from the resource and pass the "json = true" parameter? Or is there a convenient way to stop the browser from trying to render / save / open the result? (By hand, I mean, of course, not something that every user has to install in their browser.)

Also note that I am using Rails, so the header routing issue is preventing me from using the Accept header to fix this.

0


a source to share


3 answers


I may be wrong, but I suspect you forgot to return false from your event handler. This way your request is propagated to the default handler. Or, as nickf pointed out, an error causing your script to fail. Try wrapping your code in try catch and see if you have any error:



try {
   // your code
} catch(e) {
   alert(e);
}

      

+4


a source


A. It's a riot. It seems important not to comment out the "return false" statement; at the end of the event handlers.

I should have done it unintentionally by commenting out some other code near the end of the handler.



Stay tuned for over 1337 h4x0r tips in the future!

+1


a source


Well, the "correct" file extension for a JSON file is .json

, not .js

. Have you tried this?

0


a source







All Articles