Send a string with two parameters

I have this code, but this line has some problems.

var dataString = 'name='+name&'id='+id; 

what 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 right way to do this?

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> 
+4
source share
2 answers

You should:

  var dataString = { name: name, id: id} 

instead

  var dataString = 'name='+name&'id='+id; 

So, you are sure that the provided values ​​are correctly encoded in the URI.

+6
source

Try the following:

 var dataString = 'name='+name+'&id='+id; 

Instead

 var dataString = 'name='+name&'id='+id; 

And it should be inside '', and you need to add an extra + to the constant "name" and the string "& id =". So that should work.

UPD:

You can also do:

 var dataString = { name: name, id: id } 
+3
source

Source: https://habr.com/ru/post/1388050/


All Articles