我嘗試使用XMLHttpRequest()將數據發佈到url。 我寫了下面的javascript:在參數列表後傳遞php變量爲javascript函數錯誤:缺失)
function makePostRequest(url, params) {
var httpRequest = new XMLHttpRequest();
httpRequest.open("POST", url, true);
//Send the proper header information along with the request
httpRequest.setRequestHeader("Content-type", "application/json");
httpRequest.onreadystatechange = function() {//Call a function when the state changes.
if(httpRequest.readyState == 4 && httpRequest.status == 200) {
alert(httpRequest.responseText);
}
}
httpRequest.send(params);}
然後我需要將兩個PHP變量傳遞到PHP文件這個功能,一個是URL,另一個是被張貼JSON數據。
$url = "http://www.hello.com";
$json_str = "{\"format\": \"json\",
\"event\": \"revert\",
\"api_key\": \"$wgAPIKey\"}";
$editpage->editFormTextTop =
"<input type='button'value='hello' onclick='makePostRequest(\"$url\", \"$json_str\")' />";
執行後,我得到了來自螢火蟲以下錯誤:
missing) after argument list
沒有告訴你的行號?參數列表後面的「 – Ibu
」缺失)看起來像是一個JavaScript錯誤,請嘗試找出(使用Firebug或類似的工具)確切地說這個錯誤被觸發的位置;還請發佈生成的JSON字符串,如腳本所返回的那樣; – feeela
定義了'$ encoded_params'並且使用了$ json_str? –