admin管理员组

文章数量:1426797

I have the following snippet of jQuery:

var AlertType =
{
    Save: function () {
        var model = $('#alert').serialize();
        $.post('/account/alert/edit', model);
    }
}

When AlertType.Save() is called, the $.post() does not work in IE, but in all other browsers (surprising right?) I have tried to research the problem, but it is a fairly broad category or problems. I have even placed a callback function in the $.post() and tried to alert() inside of the callback, but it never hit the alert.

What might be causing this, and what would the fix be?

I have the following snippet of jQuery:

var AlertType =
{
    Save: function () {
        var model = $('#alert').serialize();
        $.post('/account/alert/edit', model);
    }
}

When AlertType.Save() is called, the $.post() does not work in IE, but in all other browsers (surprising right?) I have tried to research the problem, but it is a fairly broad category or problems. I have even placed a callback function in the $.post() and tried to alert() inside of the callback, but it never hit the alert.

What might be causing this, and what would the fix be?

Share Improve this question edited Jun 15, 2011 at 17:48 Richard Marskell - Drackir 13.4k3 gold badges35 silver badges54 bronze badges asked Jun 15, 2011 at 17:45 TheJediCowboyTheJediCowboy 9,24230 gold badges140 silver badges213 bronze badges 3
  • 7 Not so surprising, IE is not nice to us developers :( – Gabriel Commented Jun 15, 2011 at 17:46
  • 1 I have tried .errors(function(){alert('errors')}) just to see if it was hitting any errors, but I received nothing. – TheJediCowboy Commented Jun 15, 2011 at 17:48
  • Also, I was being sarcastic about being surprised lol, this is not my first experience with IE giving me trouble either – TheJediCowboy Commented Jun 15, 2011 at 17:51
Add a ment  | 

4 Answers 4

Reset to default 6

In your shoes I would break the problem down to its smallest chunk and then build up from there.

Step 1

Are you sure AlertType.Save() is being called at all? Put an alert in:

var AlertType =
        {
            Save: function () {
                alert('Save called.');
                var model = $('#alert').serialize();
                $.post('/account/alert/edit', model);

            }
        }

Step 2

If Save() is being called, try calling $.post() with null instead of model. Put a breakpoint in the code for the action method for Edit in the Alert controller. You want to make sure that the controller code is being called.

Step 3

If the controller code is being called, then you have a problem with model. (Not sure where to take it from there, sorry). If the controller code is still not being called, then try calling $.post() directly i.e. without using AlertType.Save(). So instead of:

AlertType.Save();

do the actual $.post(). You want to eliminate the chance of it being the javascript object at fault here.

Perhaps the above is overkill, but you only have to do this once and you will have learnt something if it ever happens again :) From experience IE can make you have to go around the houses in order to diagnose a problem, since IE does stupid things under the hood sometimes, that other browsers just don't do. Gotta love IE.

IE caches more aggressively than other browsers in my experience.

Try adding a random number to the query:

$.post('/account/alert/edit?r=' + (Math.random() * 999), model);

It could be security policies on the browser. Is IE blocking the use of XmlHttpRequest, possibly for all but a handful of trusted domains?

Try the $.ajax object, it work fine in IE 8 :

var ajaxobject = $.ajax(
{
    type:'POST',
    url:'/account/alert/edit',
    cache:false,
    async:true,
    global:false,
    dataType:"html",
    data:"model=" + $('#alert').serialize(),
    timeout:10000,
    success:function(recept)
    {
        alert('sucess !\nReceived data :\n' + recept);
    },
    error:function()
    {
        alert('failed.');
    }
});
if(ajaxobject == undefined)
    alert('Ajax object creation failed');

本文标签: javascriptjQuery post() not working in Internet ExplorerStack Overflow