Search code examples
javajqueryjsonstruts2struts-config

Getting JSON popup to save or open dialog instead of actual content?


My Problem: The json string returns as a file. When i call the getJSONResult action it will give me a file with the json string in it, which i can open or download.

I think it’s a configuration problem, but i can’t find it. I would be very glad if somebody could help me, thanks guys!

I have an opening form with a submit that uses an action and execute method to get the data. Then, the only way I could get jquery to work was to put an action and execute method that just returns 'success' and then I go onto my jquery grid with a 3rd action.

struts.xml

<struts>
   <package name="live" namespace="/live" extends="struts-default,json-default">
      <action name="gridAction" class="core.action.AdminAction"> 
         <result name="success" type="json"></result> 
         <result name="error">/live/YYY.jsp</result> 
      </action>
   </package>
</struts>

Solution

  • I assume you're using Internet Explorer, because I've never gotten this problem with Google Chrome (and there's even a plug-in that formats it all nicely and prettily: https://chrome.google.com/webstore/detail/chklaanhfefbnpoihckbnefhakgolnmc)

    Searching around, I've come up with two pages that offer the same solution to your problem, if indeed you are using IE:

    View JSON in Internet Explorer

    How can I convince IE to simply display application/json rather than offer to download it?

    The main gist of the solution lies here:

    Need to view JSON responses in IE?

    1. Open Notepad and paste the following:

      Windows Registry Editor Version 5.00;
      ; Tell IE 7,8,9 to open JSON documents in the browser on Windows XP and later.
      ; 25336920-03F9-11cf-8FD0-00AA00686F13 is the CLSID for the "Browse in place" .
      ;
      [HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json]
      "CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
      "Encoding"=hex:08,00,00,00
      [HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/json]
      "CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
      "Encoding"=hex:08,00,00,00
      
    2. Save document as IE-Json.reg and then run it. Note: This has been tested on Windows XP and Windows 7 using IE 7, 8, 9.