AJAX not passing POST vars in IE7/IE8
Please Note: answers along the lines of 'use jquery' or 'use (insert wellknown framework)' is not helpful. Frameworks such as jquery includes alot of extra code which is not nessary at all for what I am doing. 'But, you can include one from Google', yes that may be the case, but I prefer to keep to my own code. With that in mind, lets proceed to the problem....
I have an ajax call which doesn't pass POST vars through on IE7/IE8, but only on odd occasions. It appears to be extremely random and the majority of the time it does work.
I am had a look at jquery and cannot see much difference in the way it works compared to this custom one.
Here is the ajax function:
function GetXmlHttpObject(handler){
var objxml = null;
if(handler==null) {
handler = function() {}
}
var ProgID = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
try {
objxml = new XMLHttpRequest();
}
catch(e) {
for (var i = 0; i < ProgID.length; i++){
try {
objxml = new ActiveXObject(ProgID[i]);
}
catch(e) {
continue;
}
}
}
objxml.onreadystatechange=handler;
return objxml;
}
A function that calls the Ajax function would be like this:
function sample_ajax(object_type,object_id) {
var d = new Date();
var time = d.getTime();
var url= MYSITEURL + "my_ajax_script.php?timestamp="+time;
params = "object_type="+object_type+"&object_id="+object_id;
xmlHttp_comment_notifyreset = GetXmlHttpObject(sample_ajax_helper);//fails on safari 1
xmlHttp_comment_notifyreset.open("POST", url , true);
xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "no-cache");
xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "no-store");
xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "must-revalidate");
xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "post-check=0");
xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "pre-check=0");
xmlHttp_com开发者_开发知识库ment_notifyreset.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
xmlHttp_comment_notifyreset.setRequestHeader("Content-Type", "application/x-www-form-URLencoded");
xmlHttp_comment_notifyreset.setRequestHeader("Content-Length", params.length);
xmlHttp_comment_notifyreset.setRequestHeader("Connection", "close");
xmlHttp_comment_notifyreset.send(params);
}
I am had a look at jquery and cannot see much difference in the way it works compared to this custom one.
If the params are only failing to be sent sometimes, the problem may be in this line:
params = "object_type="+object_type+"&object_id="+object_id;
The params are not being escaped. Try this:
params = "object_type=" +
encodeURIComponent(object_type) +
"&object_id=" +
encodeURIComponent(object_id);
Use The Ultimate Ajax Object. It's concise, self-explanatory and it works on all browsers.
It's not a lot of help, but this article seems pretty detailed: https://developer.mozilla.org/en/AJAX:Getting_Started
I had a similar problem with jquery where the post parameters were not coming through. It was more to do with the way asp.net was reading post parameters. The following article demonstrates how you can extract the post parameters directly from the input stream on the request. http://www.bytechaser.com/en/functions/2jxhy5gg7w/read-ajax-post-parameters-in-asp-net.aspx. Hope it helps
I can't remember ever having seen a content-type with uppercase characters like "application/x-www-form-URLencoded", and I don't know if it would be in spec. I have seen, however, PHP ignore a POST because of an absent Content-Type header in the request. (due to a proxy that decided to leave it out)
PHP ignores any content-type it does not recognize. Chances are slim that this will fix anything, but you might give it a shot to change URL
to url
.
Try http://php.net/manual/en/reserved.variables.httprawpostdata.php in php to get the input vars like the ASP.net guy
精彩评论