jQuery ajax not working remotely on IE

蹲街弑〆低调 提交于 2019-12-13 04:27:45

问题


I have some code on the client that calls an ashx handler using $.ajax() and expects json data from the server. Everything works fine on FF, IE 6,7,8 when I run the application on a local webserver. However, when I deploy the application to a remote test server, IEs stopped working ($.ajax returns a parsererror), while FF continues to work as expected.

My first thought was that my json object must have a trialing comma which IEs hate, but that wasn't the issue as there were no trialing commas. Then, I tried changing various things like the content types from app/json to tex/plain, still the same error.

Something that I found odd is that if I fire up fiddler, then IEs will work remotely, otherwise, I get the parsererror.

Has anyone experienced something like this before? Thanks.

$.ajax({
        type: "GET",
        url: "handlers/GetAsyncResults.ashx",
        contentType: "application/json; charset=utf-8",
        data: {'from': dateFrom, 'to': dateTo, 'accountId' : aId, 'page': currentPage, 'sortField' : sortField, 'sortDirection' : sortDirection},
        dataType: "json",
        success: function(data) { GetAsyncResultsEnd(data); },
        error: function(x, y, z) { GetAsyncResultsErrorHandler(x, y, z); }
    });

EDIT: added code snippet.


回答1:


It's sorted. Thanks.

On the server end, after we called context.Response.Write('Our json data'), we then call context.Response.Flush() and context.Response.Close(). After we removed the .Flush() and .Close(), everything started working again. But I still can't explain why it's working for firefox and not IE, nor how fiddler magically made it works for IE.

Thank you.




回答2:


The only thing that has given me a parse error before it invalid JSON. You said that you checked for trailing spaces, but try running your json through - JSON Lint. That has worked well for me. It is interesting though that fiddler helps.




回答3:


Try using

contentType: 'application/json',



回答4:


Have you tried simply removing the datatype: "json"

I had this same problem and removing that fixed it. Removing that allows jQuery to "intelligently pass either responseXML or responseText to your success callback".




回答5:


In my ashx.cs method, I had to comment out the Flush() in order for this to work.

Prior to this, my json was well-formed, and everything appeared to work, but the JavaScript (JQuery .ajax POST call) would just return nothing, FireBug would not report an error, the console was not reporting an error, nothing. It all worked locally (in Visual Studio), but not when everything was on the server.

//code from the ashx.cs file...
//...after populating the response, resp:

JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
String json = jsonSerializer.Serialize(resp);

r.Write(json);
//r.Flush();  /////commented out!  It returns to the .js file correctly!
....


来源:https://stackoverflow.com/questions/1297673/jquery-ajax-not-working-remotely-on-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!