0

I build the following encoded url:

actionUrl = "MyAction?Vendors=A*A,A%26A,A%2CA"

I want to send this back to my server via an ajax call:

       $.ajax({
    url: actionUrl,        
    cache: false,
    dataType: "HTML",
    success: function (data) {
      alert('hooray');
    },
    error:function(data) {
            alert(data.responseText);
        }

});

But when this reaches my action on the server, the string is this:

        Vendors = "A*A,A&A,A,A"

I need to parse by a comma before i decode the string, but its coming to my server decoded. How do I send an encoded string to my action method via ajax? I'm using asp.net MVC4 but i think thats moot. Thanks

1
  • 1
    Why not use pipes | as a separator between the values? Commented Mar 9, 2015 at 21:33

1 Answer 1

1

Send the data as JSON like this:

var values = ["A*A","A&A","A,A"];
actionUrl = "MyAction?Vendors=" + JSON.stringify(values);

Then in ASP.net you could use system.Web.Script.Serialization.JavaScriptSerializer to serialize this into an object.

using System.Web.Script.Serialization;

var json = "[\"A*A\",\"A,A\",\"A&A\"]"; //this is the received JSON from the ajax call
var jss = new JavaScriptSerializer();
var values = jss.Deserialize<dynamic>(json);
var value1 = values[0].ToString(); //A*A
var value2 = values[1].ToString(); //A,A
var value3 = values[2].ToString(); //A&A

This way no further (unwanted) conversion takes places between the client and the server. JavaScript converts it to JSON, ASP.net converts it back to its native object notation. Thus eliminating the need to encode your characters, this will be done automatically by the browser and the server decodes it back, as you have seen.

Sign up to request clarification or add additional context in comments.

1 Comment

this worked perfectly! when you get a moment, will you look at this question..stackoverflow.com/questions/28967797/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.