jQuery AJAX POST 中的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4817161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL in AJAX POST
提问by TheNone
I want to post these variables via AJAX:
我想通过 AJAX 发布这些变量:
<div class="vIn" id="star">
<div id="inner">
<span id="1" class="disabled"></span>
<span id="2" class="disabled"></span>
<span id="3" class="disabled"></span>
<span id="4" class="disabled"></span>
<span id="5" class="disabled"></span>
<input type="hidden" id="<?php echo $_GET['cID']?>" />
</div>
</div>
With this script:
使用这个脚本:
$(document).ready(function(){
$('#inner span').click(function(){
$(this).prevAll().andSelf().addClass('enabled');
var a = $(this).attr("id");
var cID = $("#inner input").attr("id");
$.ajax({
type: "POST",
url: "ajax/rating.php",
data: "value=+a+&cID=+cID+",
success: function(msg){
alert(data);
}
});
});});
On the click event, there is no alert. Am I using the right data in $.ajax? Thanks in advance.
在单击事件上,没有警报。我在 $.ajax 中使用了正确的数据吗?提前致谢。
回答by T.J. Crowder
I would strongly recommend allowing jQuery to worry about properly encoding the string:
我强烈建议让 jQuery 担心正确编码字符串:
var a = $(this).attr("id");
var cID = $("#inner input").attr("id");
$.ajax({
type: "POST",
url: "ajax/rating.php",
data: {value: a, cID: cID}, // <== change is here
success: function(msg){
alert(msg);
}
});
Note that I'm passing data
into $.ajax
as an object, not as a string. jQuery will correctly encode it for you. See the docsfor details.
请注意,我传递data
到$.ajax
作为一个对象,而不是作为一个字符串。jQuery 会为您正确编码。有关详细信息,请参阅文档。
If you really, really want to do the encoding yourself, you have to do it explicitly:
如果您真的,真的想自己进行编码,则必须明确地进行:
data: "value=" + encodeURIComponent(a) + "&cID=" + encodeURIComponent(cID)
Also note that I'm alerting msg
, not data
, as that's what you've called the response argument to success
.
另请注意,我正在提醒msg
,而不是data
,因为这就是您所说的响应参数success
。
回答by Fenton
There is a subtle error here...
这里有一个微妙的错误......
success: function(msg){
alert(data);
}
You can fix it like this...
你可以像这样修复它......
success: function(data){
alert(data);
}
There is another error here...
这里还有一个错误......
data: "value=+a+&cID=+cID+",
I think this should be...
我觉得这应该是...
data: "value=" + a + "&cID=" + cID,
If you still don't get an alert, please shout!
如果您仍然没有收到警报,请大喊大叫!
回答by Ivan
I'm no Javascript expert but shouldn't it be data: "value="+a+"&cID="+cID
?
我不是 Javascript 专家,但不应该是data: "value="+a+"&cID="+cID
吗?
回答by Quentin
data: "value=+a+&cID=+cID+"
is trying to concatenate the values without taking care of characters with special meaning. The URL might include, for example, a &
character.
data: "value=+a+&cID=+cID+"
试图在不处理具有特殊含义的字符的情况下连接值。例如,URL 可能包括一个&
字符。
That is what it is trying to do, what it is actually doing is setting the string literal as the value. The attempt to include variables is just making them string data.
这就是它试图做的,它实际做的是将字符串文字设置为值。尝试包含变量只是使它们成为字符串数据。
You should have:
你应该有:
data: { "value": a, "cID": cID }
回答by Tjkoopa
There are a few problems with your use of $.ajax
你使用 $.ajax 有几个问题
data: "value=+a+&cID=+cID+",
'+' is the concatenation operator, what you are doing here is building the data string using the variables you set before. So you need to concat your variables with the strings "value=" etc.
'+' 是连接运算符,您在这里所做的是使用您之前设置的变量构建数据字符串。所以你需要用字符串“value=”等连接你的变量。
eg.
例如。
data: "value="+a+"&cID="+cID,