1

In my asp.net solution, I am using jquery to manipulate some html code. I then want to convert this into an xml code, and send it back to the same page via ajax. So that the c# code can read it and do server side things to it.

Whats the best way to do this? Currently, I am putting the xml code into a invisible text box, then make the asp.net ajax call, which then in c# I can read the text in the textbox.

Is there a better way?

Thanks

5
  • 1
    Does it have to be XML? I'd be tempted to use JSON instead, as it is considerably easier to work with on the client side. Commented Jun 24, 2013 at 19:09
  • you can use ajax to send the data to the backend, understand that the user can manipulate this data and you should be security conscious. Commented Jun 24, 2013 at 19:10
  • I agree with Charlie Kilian, use JSON instead of XML; if you use ASP.NET Page Methods then you can get JSON encoding for free from the server back to the client. Commented Jun 24, 2013 at 19:16
  • ok, how would I send a JSON request to the current .aspx page? And I believe its in jquery right (to send the JSON)? And how would I write the c# code to retrieve this value? Commented Jun 24, 2013 at 19:24
  • This post by Dave Ward shows exactly how to do the client and server pieces: encosia.com/… Commented Jun 24, 2013 at 21:09

1 Answer 1

2

If you use json you can do:

In your aspx page:

$.ajax({
    type: "POST",
    url: "webpage.aspx/doSomething", //doSomething is the method in the code-behind class
    dataType: "json",
    data: "{ data: 'data you want to pass to the C# method' }", // params to the doSomething method  
    contentType: "application/json; charset=utf-8",
    success: function(msg) {
    // do something with the data the C# method returned
    },
   error: function(msg) {
    alert(msg.d);
   }
});

In your webpage.aspx.cs:

[WebMethod]
    private static <returnType> doSomething(string data){
    // manipulate the data var
   return <what you want>; 
   }
Sign up to request clarification or add additional context in comments.

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.