簡體   English   中英

Ajax調用對於PHP失敗,並且對於ASP.NET正常工作

[英]Ajax call failing for PHP and working fine for ASP.NET

我正在做一個簡單的Ajax調用,該調用在ASP.NET中工作,但是在onreadystatechange函數中放置斷點時卻出現了一些奇怪的DOM異常。 例如,當您查看Google Chrome中的xmlhttp變量時,ASP.NET所做的額外頭魔術是什么,PHP無法這樣做,導致僅使用DOM Exceptions,PHP的responseText為空白。

var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        //Here is where you unwrap the data
        var arr = UnWrapVars(xmlhttp.responseText);
        if (callBackFunc) {
            callBackFunc(arr);
        }
    }
};
xmlhttp.open("POST", ajaxURL, true);
if (navigator.userAgent.toLowerCase().indexOf('msie') == -1) {
    xmlhttp.overrideMimeType("application/octet-stream");
}
xmlhttp.send("[FunctionName]" + functionName + "[/FunctionName][CanvasID]" + canvasid + "[/CanvasID][WindowID]" + windowid.toString() + "[/WindowID][Vars]" + getEncodedVariables() + "[/Vars]");

來自Ajax頁面的傳入數據就像:[root] [Vars] [windows]

我故意在[[而不是<,所以請不要再指出這一點,它可以在ASP.NET頁面上工作,但不能在PHP頁面上工作。 返回的數據與我檢查過的服務器端相同。 那么,如果任何ASP.NET都沒有執行PHP,那么缺少的標頭魔術是什么呢?

這是因為$_POST實際上是訪問通過表單發布的數據的簡寫。 它期望適當的頭。 將此行放在xmlhttp.send()之前:

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

這將迫使PHP填充$_POST數組。

或者,您可以創建一個Request實例,該實例充當php://input流的自定義包裝。 然后,您的XHR調用將不需要發送其他頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM