【发布时间】:2011-10-12 17:03:53
【问题描述】:
我确定这只是一个简单的问题,我查看了论坛但找不到特定于我的问题的示例。
基本上,我正在显示一个href 表,每个href 都有一个对ajax 方法的onclick() 调用,使用一个'get' 和一个url。
function createRequestObject(){
var req;
if(window.XMLHttpRequest){
//For Firefox, Safari, Opera
req = new XMLHttpRequest();
}
else if(window.ActiveXObject){
//For IE 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
}
else{
//Error for an old browser
alert("Your browser is not IE 5 or higher, or Firefox or Safari or Opera");
}
return req;
}
//Make the XMLHttpRequest Object
var http = createRequestObject();
function sendRequest(method, url){
if(method == "get" || method == "GET"){
http.open(method,url,true);
http.onreadystatechange = handleResponse;
http.send(null);
}
}
function handleResponse(){
if(http.readyState == 4 && http.status == 200){
var response = http.responseText;
if(response){
document.getElementById("DIVNAME").innerHTML = response;
}
}
}
对 href 的调用如下所示:
echo '<td><a href="#" onClick="sendRequest(\'get\', \''.$toPass.'\')"/>'.$variable.'</a></td>';
$toPass 变量是page.php?variable1='.$variable1.'&variable2='.$variable2.'&variable3=blah&action='.$option.'
当我传递一个变量 1 例如:'TP111010114' 它工作正常,_REQUEST['variable1'];抓取变量并通过我的 sql 请求将其推送。
如果变量包含 # 例如:'Blah #2',则唯一通过的数据是 变量 1 = 'Blah',其余变量未分配。 从看起来它在# 之后没有得到任何东西。 我尝试在 php 中对 url 进行编码,然后在我的 ajax.js 中将其取消转义,您调用 window.open('get', unescape(url), true);但我得到了相同的结果,它适用于任何不包含 # 的变量。当我尝试使用 urlencode() 对其进行编码而不是在我的 ajax.js 中对其进行解码时,请求没有通过。
编码后的请求如下:
page.php%3Fvariable1%3DTP111010114%26variable2%3D64%26variable3%3Dnew%26Action%3DOthers
如果它没有被编码,它看起来像:
page.php?variable1=TP111010114&variable2=64&variable3=new&Action=Others
我使用 firebug 在 .js 变量通过时对其进行监控,当我对其进行编码时,它看起来好像应该抓取正确的变量,但它仍然只抓取变量中的 # 之前
http://www.randomsite.ca/page.php?variable1=WF225+Amendment+#2&variable2=543&variable3=new&Action=Others
然而 php 端的这个请求仍然得到 $variable = $_REQUEST['variable1'] $variable = 'WF225 修正',其他变量未赋值。
这一定与处理#的方式有关,但我看不出我在这里缺少什么,如果有人可以提供帮助,将不胜感激。
出于隐私目的,我已重命名信息
【问题讨论】:
标签: php javascript sql ajax