How to avoid IE to download the json response [duplicate] - javascript

I am trying to upload a file and return a json response regarding properties(name, size etc) of the file. It works fine in all browsers except IE.
IE tries to download the JSON as a file !
I have IE10 and testing it on IE7 to 10 by changing browser mode and document mode from the debugger.
I am using asp.net mvc4, the file upload action have HttpPost attribute and i am returning json response using return Json(myObject);
And here are my http headers
Request
Key Value
Request POST /File/UploadFile/ HTTP/1.1
Accept text/html, application/xhtml+xml, */*
Referer http://localhost:63903/
Accept-Language en-NZ
User-Agent Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type multipart/form-data; boundary=---------------------------7dc1e71330526
Accept-Encoding gzip, deflate
Host localhost:63903
Content-Length 1377002
DNT 1
Connection Keep-Alive
Cache-Control no-cache
Response
Key Value
Response HTTP/1.1 200 OK
Server ASP.NET Development Server/11.0.0.0
Date Tue, 18 Dec 2012 23:44:19 GMT
X-AspNet-Version 4.0.30319
X-AspNetMvc-Version 4.0
Cache-Control private
Content-Type application/json; charset=utf-8
Content-Length 154
Connection Close
I tried a few suggestions but so far back to square one !

You will need to return the json as text/html since IE does not know what to do with application/json contents..
return Json(myObject, "text/html");
Not sure but it might work (and it would be more correct if it does) to use text/x-json
return Json(myObject, "text/x-json");

Even though this question is a few months old, I thought I'll add one more suggestion, just in case anyone else is using ASP.NET MVC 3 or 4 and runs into this problem.
In my experience, when IE attempts to download the Json response as a file all you have to do to correct the problem is to add a reference to jquery.unobtrusive to your view.
for example:
#Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
Once this is in place IE will no longer try to download the json response from a JsonResult controller action. No need to change the response type etc..

Related

Javascript: error 403 occurs in HTTP GET requests, some minutes after loading page

I have developed a JavaScript code that sends periodically, every 500 msec, a GET request to an HTTP server, which answers to this request with text data in JSN format.
function get(theUrl, success, failure, commData)
{
commData = commData || {};
return fetch(theUrl)
.then(function(res)
{
if (!res.ok)
throw Error(res.statusText);
return res;
})
.then(function(res)
{
return res.text();
})
.then(function(html)
{
success(html, commData);
})
.catch(function(error)
{
failure(commData);
});
}
This javascript code is part of the resources (js, css, images) that are downloaded along with the index.html from the HTTP server.
The client receives answer from the server without any problem for some minutes. Suddenly, around 15 minutes after the download of the index.html, the browser inspector shows that the client starts receiving systematically error 403 ("forbidden") to the GET request. This happens with any browser (at least with Firefox, Chrome and IE/Edge). When the 403 error occurs, the failure() function of the above code sample is triggered.
I have monitored the connection with Wireshark, and I could see that, in spite of the 403 error, actually the HTTP server is still receiving the request and is sending the (correct) answer.
It looks like that in the browser there is some timer that is started when loading the home page (index.html), and which expires after some minutes, causing the fetch() API to fail with error 403.
I need that the periodical GET can works indefinitely, without limit of time, because its purpose is to monitor the state and retrieve important diagnostic data from an embedded system.
How can I solve it?
EDIT:
First of all; I have to correct the text and the title of this post, because the "forbidden" error is not 503, but 403. I apologize for the mistake.
Secondly, I am adding some useful information. The webserver is running on RTOS in a Concerto F28M36x processor. In order to make a simulation test and to debug more easily, I adapted the code of webserver in order to have it running on a Window machine, using the MSDN C++ HTTP API (https://learn.microsoft.com/en-us/windows/win32/http/http-server-api-overview). In this scenario, the HTTP server is running on the same computer of the client (localhost). Nevertheless, the behavior (with error 403) is exactly the same, as when the webserver is running in the embedded system. Here is what is shown by the browser inspector, when the GET request is successful (result=200):
GET http://localhost/readResults.cgi
Status 200 OK
Version HTTP/1.1
Transferred 308 B (174 B size)
Referrer Policy no-referrer-when-downgrade
Response header: (157 B)
Content-Length 174
Content-Type text/plain
Date Tue, 06 Apr 2021 11:00:39 GMT
Server Microsoft-HTTPAPI/2.0
Request headers (421 B):
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
cache-control no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0
Connection keep-alive
expires Tue, 01 Jan 1980 1:00:00 GMT
Host localhost
pragma no-cache
Referer http://localhost/
Sec-GPC 1
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0
And this is shown by the browser inspector when the result is 403:
GET http://localhost/readResults.cgi
Status 403 Forbidden
VersionHTTP/1.1
Transferred 4.11 KB (3.95 KB size)
Referrer Policy no-referrer-when-downgrade
Response header: (157 B)
Cache-Control no-cache
Connection close
Content-Length 4047
Content-Type text/html; charset=UTF-8
Proxy-Connection close
Request headers (421 B):
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
cache-control no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0
Connection keep-alive
expires Tue, 01 Jan 1980 1:00:00 GMT
Host localhost
pragma no-cache
Referer http://localhost/
Sec-GPC 1
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0
As one can see, with respect to the "good" case, in the "bad" case the following additional fields are present in the response header:
Connection: close
Proxy-Connection: close
Edit - since I am not receiving any answer, could somebody please suggest me a different forum where I can post this question?
Edit - just a gentle reminder. Any suggestion?

Access Control Allow Origin header Error Only in Chrome,Safari but not Opera

I have enable Access-Control-Allow-Origin on my apache webserver. I am able to load all requests on the first load. However, subsequent loading/refreshing of the pages would leave half of the requests failed, leaving an error of,
XMLHttpRequest cannot load http://***. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
In Chrome Developer Tool, under the Network tab, enabling "Disable Cache" removes this error.
I am using AngularJS $http to call my request, I have changed the cache option to true/false but this abnormality persist. For your information, I am also using an angular-cache module, https://github.com/jmdobry/angular-cache, I have tried enabling/disabling this too, but to no avail.
Here is a copy of the request header:
GET ***Valid_Url_With_Parameters** HTTP/1.1
Host: ********
Connection: keep-alive
Accept: application/json, text/plain, */*
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Referer: http://localhost:3000/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
I have a 2 different types response header with different HTTP status codes for the failed requests.
HTTP/1.1 200 OK
Content-Length: 564121
Content-Type: application/json
Server: Apache/2.2.15 (CentOS)
Vary: Accept
Last-Modified: Fri, 16 Sep 2016 12:41:41 GMT
Connection: keep-alive
Date: Fri, 16 Sep 2016 12:43:44 GMT
HTTP/1.1 206 Partial Content
Content-Type: application/json
Server: Apache/2.2.15 (CentOS)
Vary: Accept
Last-Modified: Fri, 16 Sep 2016 13:39:05 GMT
Content-Range: bytes 29200-29200/785592
Content-Length: 1
Connection: keep-alive
Date: Fri, 16 Sep 2016 14:20:48 GMT
It seems something to do with caching. The first time the page loads, all of the request have the correct access control header. Subsequent refresh/reload removes this header. How can I continue using caching and keep the access control header?
Edit:
Just to add on, I tried using Safari, the same abnormality appeared. It load on the first try but subsequent loading will throw a no access control allow origin error. However, this time, the response header in Safari shows blank.
Edit 2:
Abnormality appears in Firefox as well but not in Opera Beta version 40.0.2308.52. I am beginning to wonder if this is the cause of the browser. Access-Control-Allow-Origin header is present too.
Response Header in Opera
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Date: Sat, 17 Sep 2016 06:56:53 GMT
Server: Apache/2.2.15 (CentOS)
Vary: Accept
Transfer-Encoding: chunked
Edit 3:
I would like to add that the requests that I'm pulling is a large amount of data. I am pulling the last 7 days history of data, there are about 20 JSON files, each ranging from 500KB to 1000KB. I noticed that when I change the query parameter to pull in a small history of data, this error does not happen.
Any help is much appreciated.
One work around I've found is to dynamically generate a javascript file on the server as resource included into your page. This doesn't require AJAX the which gets blocked by CORS and can be cached.
We had this same problem in Chrome when returning a specific value in the 'Access-Control-Allow-Origin' header. We were able to solve it by also including Vary: Origin in the response headers.
It appears Chrome considers response headers, including 'Access-Control-Allow-Origin', as part of the cached response. So if it requests the same file from a different origin, that header from the cache will not match. But if you include Vary: Origin it lets Chrome know the response (just this one header in our case) will be different based on the Origin of the request.
This page on MDN explains it:
If the server sends a response with an Access-Control-Allow-Origin
value that is an explicit origin (rather than the "*" wildcard), then
the response should also include a Vary response header with the value
Origin — to indicate to browsers that server responses can differ
based on the value of the Origin request header.
I would recommend you to add chrome extension in your browser
Allow-Control-Allow-Origin: *

IE displays or tries to download JSON when it should be doing an action

EDIT: It appears the file jquery.unobtrusive-ajax.js is not being loaded into IE when IE runs. It is on the server and I can see it in the scrips but I can't find it in the IE scripts..
I have searched several links and many have answers of changing the content-type to either text/plain or text/html.
My issue is unique since it only happens on the development server with ie.
Locally with IE and chrome is works fine. On the server with Chrome it works fine, IE it does not.
I am returning a JSON object
C#
return Json(new { success = true, approved = true, redirect = Url.Action("Index", "Dashboard") }, "text/html");
JS
function Redirect(data) {
debugger;
data = JSON.parse(data);
if (data.success) {
if (data.approved == false) {
//some code
}
else {
//some code
}
}
However all I get on my screen in the JSON object displayed out in plain text..
{"success":true,"approved":true,"redirect":"/Home/Index/"}
I am stumped as to why this is happening. I have tried to change the content type to all different kinds of versions. And this is only happening while running from the server and not locally.
Here is also the responses.
Response
Key Value
Cache-Control private, s-maxage=0
Content-Type text/html; charset=utf-8
Date Fri, 17 Jul 2015 20:54:49 GMT
Response HTTP/1.1 200 OK
Server Microsoft-IIS/8.0
Transfer-Encoding chunked
X-AspNet-Version 4.0.30319
X-AspNetMvc-Version 5.2
X-Powered-By ASP.NET
X-SourceFiles =?UTF-8?B?QzpcVXNlcnNccnF1ZWVuXFNWTlxGT09EXEZvb2RBcHA=?=
Request
Key Value
Accept text/html, application/xhtml+xml, */*
Accept-Encoding gzip, deflate
Accept-Language en-US
Cache-Control no-cache
Connection Keep-Alive
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Thanks in advance.
As silly as this is..The jquery.unobtrusive-ajax.js file was removed and re added and it works. Such a trivial fix...

How can I do POST query knowing its headers and its POLL respond?

I am trying to understand what happens when I click a button, I get a POST request to which I get POLL request (the result). Now I want to be able to do the request without clicking, without using event triggers, I want to be able to do the request by typing it to the Firebug -command line. The button has a keydown -event with function "fn.apply(object, args)" which calls some object and args which I don't know yet.
Without the information, is there some way to redo the just done POST request without clicking?
Related post here where I am asking about events, now the scope is just trying to replicate POST -request to get POLL request.
I think I have all relevant information stored to the browser with things such as session ID so I need just to trigger the function which triggers the POST query or to trigger the POST query directly.
How can I do the POST query?
If I cannot do the POST query again with these information, what else do I need to do a POST query?
What does the Silverlight usage with SVC infer? Do I need to format the query somehow or is the POST query standardized?
POST/POLL URLS
http://game.com/Presentation/Service.svc/ajaxEndpoint/StartSearch
http://game.com/Presentation/Service.svc/ajaxEndpointPoll
POST StartSearch
Request Headers
Host game.com
User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110422 Ubuntu/10.04 (lucid) Firefox/3.6.17
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type application/json; charset=UTF-8
X-Qooxdoo-Response-Type application/json
Referer http://game.com/index.aspx
Content-Length 110
Cookie __utma=30514082.25780803.1305937283.1306259330.1306374895.6; __utmz=30514082.1306376876.6.8.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=JSON%20site%3Agame.com; __utmb=30514082.9.10.1306374895; __utmv=; __utmc=30514082; s_sq=%5B%5BB%5D%5D; s_cc=true; s_ria=flash%2010%7Csilverlight%20not%20detected
Pragma no-cache
Cache-Control no-cache
POLL Request Headers
POST /Presentation/Service.svc/ajaxEndpoint/Poll HTTP/1.1
Host: game.com
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110422 Ubuntu/10.04 (lucid) Firefox/3.6.17
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/json; charset=UTF-8
X-Qooxdoo-Response-Type: application/json
Referer: http://game.com/index.aspx
Content-Length: 295
Cookie: __utma=30514082.25780803.1305937283.1306259330.1306374895.6; __utmz=30514082.1306376876.6.8.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=JSON%20site%3Agame.com; __utmv=; __utmc=30514082; s_sq=%5B%5BB%5D%5D; s_cc=true; s_ria=flash%2010%7Csilverlight%20not%20detected
Pragma: no-cache
Cache-Control: no-cache
[Update]
Directed by reply to Ajax, I think it may work something like below. It runs but do not work as supposed (debugging...):
var client = new XMLHttpRequest();
client.open("POST", "http://game.com/Presentation/Service.svc/ajaxEndpoint/StartSearch", true);
client.setRequestHeader("Content-Type", "application/json; charset=utf-8");
client.send('{"session":"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx","cityid":"787878788","maxGold":0,"resType":0,"maxTime":86400}');
alert( client.getResponseText );
// document.getElementById("myDiv").innerHTML= client.responseText;
Format of the JSON in POLL reply from POST request
One format of the JSON, apparently having some sort of validating stuff before request. It is received after the POST query as a POLL. If I can understand right, I need a new request ID, new time and I may need to update something.
{"session":"xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx","requestid":"111","requests":"UA:\fMM:888,0,\fCAT:1\fSERVER:\fALLIANCE:\fQUEST:\fEE:\fPLAYER:\fCITY:111111111\fWC:\fWORLD:\fVIS:c:111111111:0:-1111:-111:222:444\fUFP:\fREPORT:\fMAIL:\fFRIENDINV:\fTIME:11111111111111\fCHAT:\fSUBSTITUTION:\fINV:\fALL_AT:\fMAT:333333333\fSEARCH:\f"}
jQuery option
many answers suggest jQuery so I ran the jQuery source here in Firebug but not getting it working, trying. It is probably the easiest way to do the POST query. If someone could tell some sort of vizualiser to see my POLL queries, it would be greatly appreciated (not knowing certain which is wrong, using firebug's profiler and debugger). It may be that I do not catch the output but it is coming, currently receiving undefined as reply.
WOW you looked to much into what you computer is doing and what you need to do,
Go look up AJAX (XMLHttpRequest) -- http://www.w3schools.com/ajax/default.asp
For best result use jQuery
$().ajax(
url:""
data:{"pollPostName":"pollactiveID"}
dataType:"json"
);
If you give me the full link for how i can access the poll i will write it for you. using what every frameworks you have or dont have if native is needed
http://api.jquery.com/jQuery.post/
write a function that triggers $.post
then you can use that function in the JS command line.

empty response body in ajax (or 206 Partial Content)

I'm feeling completely stupid because I've spent two hours solving task which should be very simple and which I solved many times before. But now I'm not even sure in which direction to dig.
I fail to fetch static content using ajax from local servers (Apache and Mongrel). I get responses 200 and 206 (depending on the server), empty response text (although Content-Length header is always correct), firebug shows request in red.
Javascript is very generic, I'm getting same results even here: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first
(just change document location to 'http://localhost:3000/whatever')
So, it's probably not the cause.
Well, now I'm out of ideas. I can also post http headers, if it'll help.
Thanks!
Response Headers
Connection close
Date Sat, 01 May 2010 21:05:23 GMT
Last-Modified Sun, 18 Apr 2010 19:33:26 GMT
Content-Type text/html
Content-Length 7466
Request Headers
Host localhost:3000
User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Referer http://www.w3schools.com/ajax/tryit_view.asp
Origin http://www.w3schools.com
Response Headers
Date Sat, 01 May 2010 21:54:59 GMT
Server Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8l DAV/2 mod_jk/1.2.28
Etag "3d5cbdb-fb4-4819c460d4a40"
Accept-Ranges bytes
Content-Length 4020
Cache-Control max-age=7200, public, proxy-revalidate
Expires Sat, 01 May 2010 23:54:59 GMT
Content-Range bytes 0-4019/4020
Keep-Alive timeout=5, max=100
Connection Keep-Alive
Content-Type application/javascript
Request Headers
Host localhost
User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Origin null
UPDATED:
I've found a problem, it was about cross-domain requests. I knew that there are restrictions, but thought they're relaxed for local filesystem and local servers. (and expected more descriptive error message, anyway)
Thanks everybody!
It seems lake a caching problem only. Just delete cache inside of Internet Explorer and repeat your experiment. All HTTP GET requests will be cached. IE cache also ajax responses. If you don't like it you can append the URL with the text like '?p=blala'. Where 'p' is a name which will be interpret as a name of a parameter and text 'blala' must be unique in every request. Typically one use (new Date).getTime() construct to generate such 'blala'. IE will "think", that URL is new and will always send request to server.
UPDATED: Caching of static data will be made in all browsers, especially if Web server explicitly allow this: see Cache-Control: max-age=7200, public, proxy-revalidate is the response from server. Just try to go to http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first and modify the line
xmlhttp.open("GET","ajax_info.txt",true);
to
xmlhttp.open("GET","ajax_info.txt?p=" + (new Date).getTime(),true);
then click on "Edit and Click Me >>". Then if you click on "Change Content" button you will see in the HTTP traffic full data contain. I don't see any time 206 code. What it means if Response has "Accept-Ranges: bytes" and "Content-Range" like "bytes 0-4019/4020" inside of HTTP header you can read on http://benramsey.com/archives/206-partial-content-and-range-requests/

Categories