1

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>
Esh
  • 836
  • 5
  • 16
  • 43
  • your question is not very clear to me..what problem you actually getting? are you suing struts2-json plugin? – Umesh Awasthi Jul 24 '12 at 06:43
  • hi Umesh , let me explain my situation first . i am using struts2 jquery for UI development and Struts2 for the flow . the issue am facing is , whenevr am trying to login my page , instead of the grid to display am getting a download popup of the JSON contents . I dont know where i done the mistake ... And am using struts2-json plugin – Esh Jul 24 '12 at 06:50
  • 1
    can you show the code and xml of relevant part? you need to use result type as `JSON` – Umesh Awasthi Jul 24 '12 at 07:03
  • The below is my configuration in struts.xml /live/useradminerror.jsp – Esh Jul 24 '12 at 07:08
  • its better to update you post with code from your action and xml file to see it completely, also why you doing this `` as data will be sent as JSON so `` will also work – Umesh Awasthi Jul 24 '12 at 07:20
  • sorry Umesh , ik cant post the code in the action class , due to security reason in my Org . can u please suggest some other steps to avoid this bug.... – Esh Jul 24 '12 at 07:28
  • can you explain it a bit more, on the first you are showing a page with loin screen and when you click login button you are sending request to server using Jquery and getting response back from the server as JSON, but its opening a pop-up? – Umesh Awasthi Jul 24 '12 at 07:31
  • it is difficult to help you if you can't post your code. – Uchenna Nwanyanwu Jul 24 '12 at 08:03
  • Ok , what part of my code you want ,,, since my action class contains more than 700 lines of code – Esh Jul 24 '12 at 09:08
  • @UmeshAwasthi , what u are asking is perfect , am getting download popup dialog , like the below two images ....http://www.google.co.in/imgres?q=JSON+download+as+file&um=1&hl=en&client=firefox-a&sa=N&rls=org.mozilla:en-US:official&biw=1366&bih=575&tbm=isch&tbnid=KmaETq1pqGWqJM:&imgrefurl=http://www.mkyong.com/webservices/jax-rs/download-json-from-jax-rs-with-jaxb-resteasy/&docid=A9pzMEPWONMK7M&imgurl=http://www.mkyong.com/wp-content/uploads/2011/07/jaxb-json-resteasy.png – Esh Jul 24 '12 at 09:15
  • @UmeshAwasthi and like http://www.google.co.in/imgres?q=JSON+download+as+file&um=1&hl=en&client=firefox-a&sa=N&rls=org.mozilla:en-US:official&biw=1366&bih=575&tbm=isch&tbnid=6dCGCGeSa7di_M:&imgrefurl=http://www.dotnetcurry.com/ShowArticle.aspx%3FID%3D596&docid=ecziYCCdZg_cPM&imgurl=http://www.dotnetcurry.com/images/silverlight/26102010image_1.jpg – Esh Jul 24 '12 at 09:18
  • hi @UmeshAwasthi please suggest me what to do next , i tries searching in google , but no use , i cant find the reason y the download dialog is coming – Esh Aug 03 '12 at 04:45
  • `sorry Umesh , ik cant post the code in the action class , due to security reason in my Org` possibly you can write something on your own which you can publish. This should be more helpful – Kowser Aug 08 '12 at 14:14
  • Can you use something like Fiddler to inspect the response headers? Specifically, look for the response header "Content-Disposition" and see whether it has a value of "attachment" with or without a filename. – Rajesh J Advani Aug 09 '12 at 07:39

2 Answers2

2

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.
Community
  • 1
  • 1
Derek
  • 134
  • 1
  • 8
1

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.

Quaternion
  • 10,380
  • 6
  • 51
  • 102