2

my problem is following. I try to parse some data via ajax, passing the data to my controller:

AJAX

$.ajax({
            type: "GET",
            url: "ParseOrganizaitonPath",
            data: {
                organizationPath: $('#organizationPath').val()
            },
            success:
                    function (data) {
                         //data is from type string with value "System.string[]" 
                         //but should be from type System.string[] 
                        });
                    }
        });

Controller

public string[] ParseOrganizaitonPath(string organizationPath)
{
    List<string> organizations = organizationPath.Split('/').ToList();

    return organizations.ToArray();
}

I am reaching the controller method and in it everything is fine, but the data that is comming back (ajax part, success method) is just a string ("System.string[]", data[0] S, data[1]y data[2]s...) but not the data I want. (For example if i pass the input "test/one" I want to have as result data[0] test, data[1] one)

Hope you understand what my Problem is.

Thanks in advance!

Julian

2 Answers 2

2

Have to tried to use the JavaScriptSerializer? Have a look at this example:

public string ParseOrganizaitonPath(string organizationPath)
{
    List<string> organizations = organizationPath.Split('/').ToList();
    System.Web.Script.Serialization.JavaScriptSerializer oSerializer = 
             new System.Web.Script.Serialization.JavaScriptSerializer();
    return oSerializer.Serialize(organizations);
}

To deserialize the JSON string with JavaScript you can use the parse function:

var array = JSON.parse(data);
Sign up to request clarification or add additional context in comments.

3 Comments

As this is a array converted into a string this is not what im looking for. Sorry.
Have a look at my edit which shows how to deserialize the string into an array.
Pretty cool, this is working fine, but isn't there another way to do it without serializing the return value?
1

I found a way where you don't have to serialize it (on c# site) and parse it (on javascript site)

Just use the JSON Method that is inherited from the Controller and return an JsonResult:

public JsonResult ParseOrganizaitonPath(string organizationPath)
{
    List<string> organizations = organizationPath.Split('/').ToList();

    return JSON(organizations);
}

On the client site (javascript) you have to use JSON.stringfy(dataObject) for data that you want to send to your controller-method.

$.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            url: "ParseOrganizaitonPath",
            data: JSON.stringify(myDataObject),
            success:
                    function (data) {
                         //use your data
                        });
                    }
        });

That's how it worked for me.

Good luck!

Julian

Comments

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.