The multi-page POST form causes Firefox to request JSON Save

I have a problem that occurs in IE 8 and Firefox 6.0, but not in Chrome 17.0.9. When I post frmMain below, I post it to a test page that simply returns a simple JSON string with ContentType: application/json; charset=utf-8 ContentType: application/json; charset=utf-8 . The problem is that IE and FF will offer me to save the JSON that is returned from the server, and not hit the success method in my jquery code. But it’s strange if I omitted <input name='File_1' type='file' /> in the published form, then IE and FF will not tell me to save my JSON, and my jquery success code will run.

So, it seems that the hosted content has a footing (in IE and FF) about how the browser responds to the returned payload. Through Fiddler, I checked that in each case the returned payload is exactly the same.

Any ideas?

SOLUTION FOUND: See my answer below. From what I can compile, "text / html" is the best type of cross-browser content to return when jquery / ajax / json is executed.

THE CODE

 <script> $(function () { $('#btnSave').click(function () { $('#frmMain').ajaxSubmit({ success: function (data, statusText, xhr, $form) { alert('test success'); }, fail: function (data, statusText, xhr, $form) { alert('test fail'); } }); }); }); </script> <body> <form id='frmMain' action='/test' method='post'> <!--Omit the file input below to make it work--> file: <input name='File_1' type='file' /><br /> name: <input name='json' value='{"id":5}' /><br /> <input type='button' id='btnSave' value='Save' /> </form> </body> 

CALL WITH FILE DOWNLOAD (REASON FAULT): enter image description here

CALLED WITHOUT A FILE DOWNLOAD (WORKS): enter image description here

WHAT TO SEE IN IE: enter image description here

WHAT YOU NEED TO SEE IN FF: enter image description here

+6
source share
2 answers

After TON trial and error, I came across this SO post , where @ItsJason recommended setting the ContentType server to "text / html". This fixed the problem, and my jQuery code still recognized the returned payload as JSON when it called my callback method. so the lesson learned: for full cross-browser compatibility, when returning JSON to jquery during ajax postback don't use "application / json", use "text / html" !!

+2
source

You may need to specify that the return type is json.

It looks like you are using a plugin. In any case, there is information about setting the type of returned data: http://jquery.malsup.com/form/#options-object

0
source

Source: https://habr.com/ru/post/912610/


All Articles