开发者

jQuery $.post() not working in Internet Explorer

开发者 https://www.devze.com 2023-03-13 10:13 出处:网络
I have the following snippet of jQuery: var AlertType = { Save: function () { var model = $(\'#alert\').serialize();

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 开发者_如何学Gobroad 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?


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');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号