admin管理员组文章数量:1023738
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 }
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 }
本文标签: javascriptsend string with two parametersStack Overflow
版权声明:本文标题:javascript - send string with two parameters - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598049a2158289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论