Using “&” and “+” in Querystring

前端 未结 4 403
礼貌的吻别
礼貌的吻别 2021-01-27 09:18

In call the following URL in Javascript.

var par = \"Participant.aspx?ID=\" + Id + \"&NAME=\" + Name+ \"&FIRSTNAME=\" + Firstname;

Some

4条回答
  •  离开以前
    2021-01-27 10:01

    Remember that the contents of the query string (both the names and values) must be correctly URI-encoded. If that line is in JavaScript, you'd do that like this:

    var par = "Participant.aspx?ID=" + encodeURIComponent(Id) +
                "&NAME=" + encodeURIComponent(Name) +
                "&FIRSTNAME=" + encodeURIComponent(Firstname);
    

    (Technically, again, the names should be encoded too, but "ID", "NAME", and "FIRSTNAME" encode to exactly the same thing, so I didn't use encodeURIComponent on them.)

    See AntP's comment re the plus signs and spaces:

    "With URLEncode is the problem that if the Querystring contains spaces between the Name and the "plus" sign, the spaces also get converted to "plus" signs." - that is what should happen. A plus sign denotes a space. An encoded plus sign denotes a plus sign.

提交回复
热议问题