IE not triggering jQuery Ajax success

后端 未结 13 1096
心在旅途
心在旅途 2020-12-06 10:50

I\'m working on a script to load some images async using jQuery.

Here is a code snippet of the function that loads the images -

try{
    for(img in i         


        
相关标签:
13条回答
  • 2020-12-06 11:12

    if you success is:

    Success: function (data) {
                        if (data != null) {
                            alert(data);
                           ;
                        }
                    },
    

    change for this:

     success: function (data, textStatus, XMLHttpRequest) {
                    alert(data.d);
                } 
    
    0 讨论(0)
  • 2020-12-06 11:14

    Another thing you can try, very much the same as the solution from John Saunders, is try "jsonp" as the content type, instead of json - for a json data type expected response. I had to try this with IE to get my bug to go away, which was the normal code working in every other browser, except IE.

    Here's my code that worked:

    $.ajax({
        url: request_url,
        data: { var1: 'stuff' },
        dataType: "jsonp",
        type: "get",
        async: true,
        cache: false,
        success: function(collections) {
             // handle result
        }
    });
    

    Cheers.

    0 讨论(0)
  • 2020-12-06 11:17

    In the end I had to create a separate function for IE browsers.

    The objective was to test for an image at a location so the code looked something like -

        //get the images from the array
    for(img in imgsArray){  
    
        if($.browser.msie){     
        /* DOM manipulation method */
        //IE has problems with ajax so try this instead
        //create a new image object with unique ID
        var testImg = $("<img/>");
        var imgID = "imgID"+img;
    
        //hide it..
        testImg .css({ "visibility":"hidden" });
        testImg .attr("src",imgsArray[img]);
        testImg .attr("id",imgID);
    
        //.. and insert it into the DOM or else test will always fail
        //because it does not exist as far as browser is concerned
        $("body").append(testImg );
    
        //test for image
        if(document.getElementById(imgID).width > 60){
            //do something useful
        }
    }
    

    It is not really the answer to my question but it's a functional work around.

    0 讨论(0)
  • 2020-12-06 11:18

    I'd suggest using Charles Proxy to see what's going on - i.e. is the request going out at all? What's the response?

    Also, I think there might be some Exception thrown, so why don't you add some alerts in the Catch section to see what happens?

    0 讨论(0)
  • 2020-12-06 11:23

    set 'cache: false' inside .ajax config works for me :)

    0 讨论(0)
  • 2020-12-06 11:25

    A simple fix of this problem is to provide the jQuery setting dataType : 'text' or dataType : 'xml' or dataType : 'json' or any other available response type.

    I had same problem, but it's working fine after specifying the dataType setting in the .ajax call.

    IE is really not an intelligent browser, it doesn't assume the default value string.

    Try it... good luck.

    0 讨论(0)
提交回复
热议问题