Pergunta

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>
Foi útil?

Solução

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.

Outras dicas

There is no issue (struts2 issue that is), this is because you are using chrome and calling an action which returns a json result without using ajax. Some browsers would only offer you a download dialogue personally I really like that chrome shows the json string in the browser window without any fuss... it is great for debugging.

Update: I didn't look at the first screen shot of IE, where you are getting the download dialogue.

To prevent this... I assume you'll want to call the action asynchronously and since you mention using jquery (in your comments) you should probably look to jQuery.getJSON()

If this solution does not do enough for you please post the code you are using for the view because the issue lies there.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top