Patrick
Patrick

Reputation: 4298

Not able to pass entire html data at C# due to & symbol in the data

I have a ajax post method where i pass json data using stringify and pass it to c# server side using ajax call.

The data consists of several rows of html table. The 2nd row has '&' in the input. So any data after & is not passed to the server side. only the data before '&' is passed

for e.g.

[{"FName":"Shesh","LName":"Chari","Department":"Automation"},
 {"FName":"Uma","LName":"uri","Department":"Invest & Ops"},
 {"FName":"Book","LName":"Mac","Department":"Ops"},
 {"FName":"Hard","LName":"Core","Department":"Invest"}]

As you can see above, instead of passing entire data as it is, it is passing this truncated data shown below. i guess it has to be due to & in the data. Any advice pls ?

[{"FName":"Shesh","LName":"Chari","Department":"Automation"},
 {"FName":"Uma","LName":"uri","Department":"Invest

Here is the JSON sent by ajax

var submitData = "=" + JSON.stringify($scope.gridOptions.rowData);

    $.ajax({
        type: 'POST',
        url: 'URL',
        data: submitData
    });

Upvotes: 1

Views: 99

Answers (3)

KaffineAddict
KaffineAddict

Reputation: 446

I would encode the ampersands before you send them to the server. You can do this by calling a simple regex on the columns that could contain an ampersand or by calling it on the entire string that you stringified.

var searchStr = "&";
var replaceStr = "%26";
var re = new RegExp(searchStr, "g");

Here is a really good post explaining what is happening and has a solution posted.

Stringify with Ampersand

Upvotes: 0

James Dev
James Dev

Reputation: 3009

Sanitise your Json data first:

var sanitiseString = $scope.gridOptions.rowData.replace("&", "&")
var submitData = "=" + JSON.stringify(sanitiseString);

$.ajax({
    type: 'POST',
    url: 'URL',
    data: submitData
});

Upvotes: 1

spectacularbob
spectacularbob

Reputation: 3228

& is used in html as an escape sequence to display characters that are reserved in html (i.e. < becomes &lt; and > becomes &gt;). Because of this, & is reserved itself. You will need to sterilize your JSON string by converting all & to &amp;

See Html entities

Upvotes: 1

Related Questions