I have this code, but this line has some problem.
var dataString = 'name='+name&'id='+id;
what is sent (firebug):
'id ' id
'name' name
The line above works correctly if i do: var dataString = 'name='+name;
However, i need to pass two parameters. What is the correct way to do that?
code
<script type="text/javascript">
$(function () {
$(".vote").click(function () {
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'name='+name&'id='+id;
if (name == 'up') {
$.ajax({
type: "POST",
url: "url.php",
data: dataString,
cache: false,
success: function (html) {
}
});
return false;
});
});
</script>
I have this code, but this line has some problem.
var dataString = 'name='+name&'id='+id;
what is sent (firebug):
'id ' id
'name' name
The line above works correctly if i do: var dataString = 'name='+name;
However, i need to pass two parameters. What is the correct way to do that?
code
<script type="text/javascript">
$(function () {
$(".vote").click(function () {
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'name='+name&'id='+id;
if (name == 'up') {
$.ajax({
type: "POST",
url: "url.php",
data: dataString,
cache: false,
success: function (html) {
}
});
return false;
});
});
</script>
Share
Improve this question
asked Dec 26, 2011 at 19:14
daniel__daniel__
11.9k16 gold badges67 silver badges91 bronze badges
2
-
&
goes in the string.'name='+name&'id='+id;
should really be'name='+name+'&id='+id;
– nico Commented Dec 26, 2011 at 19:19 -
BTW, in an event handler
$(this).attr('id') === this.id
. – Alnitak Commented Dec 26, 2011 at 19:22
2 Answers
Reset to default 6You should do:
var dataString = { name: name, id: id}
instead of
var dataString = 'name='+name&'id='+id;
So that you are sure that the supplied values are correctly URI encoded.
Try this:
var dataString = 'name='+name+'&id='+id;
Instead of
var dataString = 'name='+name&'id='+id;
The & should be inside '', and you need extra + to concat "name" variable and '&id=' string. So this should work.
UPD:
You can also do:
var dataString = { name: name, id: id }
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745598050a4635234.html
评论列表(0条)