2

I've been trying out the jquery form plugin and it works wonderfully. Oh, except for IE8. It's always ie8.

Anyways, on the success response callback, ie8 prompts me to download the response instead of actually calling the success function.

Here is what my javascript code looks like

 $("#form1").ajaxForm({
                url: "http://localhost:4887/api/file/POST",
                type: "POST",                 
                success: function (data)
                {
                    //response stuff here                   
                }
            });

I've tried specifying the datatype for the ajax form, but woe is me, it didn't work

The only thing I am returning from the server is just a string. Once again, IE8 prompts me to download this string instead of just calling the success function. After some research, I understand that I might have to modify the http headers? Could anyone shed some light on this? Or give another way of going about this?

UPDATE Here is a brief look at the C# controller

public class fileController : ApiController
{     
    public JsonResult POST()
    {
        HttpPostedFile file = null; 


       string encodedString = //do stuff here to get the base64 string

        ModelName obj = new ModelName();

        obj.characters = encodedString;
        JsonResult result = new JsonResult();
        result.Data = obj;
        result.ContentType = "text/html";

        return result;

    }

Request Headers...

Accept application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, /

Accept-Language en-US

User-Agent Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; .NET CLR 1.1.4322)

Content-Type multipart/form-data; boundary=---------------------------7dd3e622907b6

Accept-Encoding gzip, deflate

Proxy-Connection Keep-Alive

Content-Length 300

Response Headers HTTP/1.1 200 OK Cache-Control no-cache

Pragma no-cache

Content-Type application/json; charset=utf-8

Expires -1

Server Microsoft-IIS/8.0

X-AspNet-Version 4.0.30319 X-Powered-By ASP.NET

prawn
  • 2,643
  • 2
  • 33
  • 49
  • Can you monitor the request and response in Fiddler or the IE dev tools (I don't remember if IE8 had a working network tab). If you post the request and response headers, that may help. – Joe Enos Oct 06 '13 at 20:24
  • Also, is this regular MVC or WebAPI? (Does your class inherit from `Controller` or `ApiController`? – Joe Enos Oct 06 '13 at 20:25
  • It's WebAPI. So ApiController – prawn Oct 06 '13 at 20:46
  • Duplicate: http://stackoverflow.com/questions/8892819/ie8-treats-json-response-as-file-and-tries-to-download-it – Chad Oct 07 '13 at 02:29
  • tried it. Added HttpContext.Current.Response.ContentType = "text/html"; and the problem still persists. unless I'm missing something – prawn Oct 07 '13 at 05:04

1 Answers1

1

try this:

[HttpPost]
public JsonResult POST()
{
    HttpPostedFile file = null; ;
    string encodedString = //get the file contents, and get the base64 encoded string        
    ModelName obj= new ModelName();
    obj.characters = encodedString;
    return   Json(obj, "text/html");

}

Update:

Or change the content-type
Response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");

example:

public JsonResult POST()
    {
        HttpPostedFile file = null; ;
        string encodedString = //get the file contents, and get the base64 encoded string        
        ModelName obj= new ModelName();
        obj.characters = encodedString;
        Response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
        return   Json(obj, "text/html");

    }
Dvir
  • 3,287
  • 1
  • 21
  • 33
  • I've tried it. Still prompts me to download. It's something with the response headers – prawn Oct 06 '13 at 20:07
  • see my edit. It seems like Ie 8 doesn't know what is application/json – Dvir Oct 06 '13 at 20:12
  • I might be horribly ignorant, but doesn't that use the System.Json dll? Which is for silverlight? I've tried JsonConvert.SerializeObject(obj), but it still doesn't work – prawn Oct 06 '13 at 20:12
  • are you using asp.net? mvc? – Dvir Oct 06 '13 at 20:15
  • Or rather, how would I specify the "text/html" part? It's MVC4 so it's using the newtonsoft.Json reference – prawn Oct 06 '13 at 20:15
  • POST is return `ActionResult`? post the code of this action please – Dvir Oct 06 '13 at 20:16
  • i've posted the relevant parts of the controller – prawn Oct 06 '13 at 20:21
  • see my edit. I changed the prototype of the function and return Json. – Dvir Oct 06 '13 at 20:24
  • that Json() call...what is that under? I wasn't able to set it up exactly like that, so I tried something different. Updated, though it's still not working – prawn Oct 06 '13 at 20:51
  • JsonResult is from `System.Web.Mvc` – Dvir Oct 06 '13 at 21:01
  • Yes I've included that. It's just not liking the Json(obj, "text/html"); part of it so i've tried another, seemingly similar approach, but it still yields the same result – prawn Oct 06 '13 at 21:08
  • Ok so add this it must be working `response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");` from `System.Net.Http.Headers` – Dvir Oct 06 '13 at 21:36
  • I'm sorry, but where would that line go? – prawn Oct 06 '13 at 22:11
  • But the variable 'response' isn't being used anywhere. it would throw an exception – prawn Oct 06 '13 at 22:26
  • It's static like Request. Is it throwing an excpetion when you trying to access this variable? – Dvir Oct 06 '13 at 22:44
  • Yea I see what you mean. I see the Request variable, but not the response one. It just says "the name response does not exist in the current contest" – prawn Oct 06 '13 at 22:47
  • try Response not response – Dvir Oct 06 '13 at 23:02
  • use `System.Net.Http` – Dvir Oct 06 '13 at 23:09