chris
chris

Reputation: 3911

Passing ampersands in Javascript through AJAX

I am making an AJAX call with JQuery to a .Net application. The value that is being passed sometimes to my ajax call is &.

var searchValue = '&';
var url = "method=searchvariables&searchvalue="+searchValue;

url += "&position=1";

On the .NET side, the searchVale is always null.

How do I pass in the ampersand so it gets recognized by the .Net code?

Upvotes: 2

Views: 1547

Answers (2)

edfuh
edfuh

Reputation: 490

You can leverage jQuery to build the query string by passing a data object. That will take care of encodeURIComponent for you

$.ajax(URL, {
    data: {
        method : searchvariables,
        searchvalue : searchValue,
        position : 1
    }
});

Upvotes: 1

Quentin
Quentin

Reputation: 943579

Some characters have special meaning in URIs, use encodeURIComponent if you want to use them as data.

Upvotes: 8

Related Questions