最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - send string with two parameters - Stack Overflow

matteradmin7PV0评论

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
Add a ment  | 

2 Answers 2

Reset to default 6

You 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 }
Post a comment

comment list (0)

  1. No comments so far