0

I am trying to use $.ajax.post using:

       $.ajax({
                type: "POST",
                url: "http://localhost/products/SaveXML.aspx",
                data: { name: "John", location: "Boston" }
            }).done(function (msg) {
                alert("Data Saved: " + msg);
            });               
        });

SaveXML lookes like:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="c#" runat="server">

public void testMethod()
{              
    string sayHello = "hello world";
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
      <title></title>
   </head>
   <body>
   <form id="form1" runat="server"></form>
   </body>
</html>

Eventually, I want to pass in some XML and have SaveXML handle it.

Does the code need to be in a code-behind? Does it need to be marked as a web method?

Can someone show me what this should look like please?

Thanks

2
  • You can simply use GenericHttpHandler and implement processrequest method. In your process request method do xml writing logic based on the data you received in HttpContext parameter and send some Response code or key to identify the success in ajax. Commented Nov 20, 2012 at 11:43
  • Can you post an example please? Commented Nov 20, 2012 at 12:47

2 Answers 2

1

You can use ASP.NET Page Methods with jQuery.

Check this:

Using jQuery to directly call ASP.NET AJAX page methods

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

1 Comment

Thanks, I'm looking at this but still no joy so far.
0

The code needs indeed to be server side code (which doesn't mean you have to have a code behind file - what you have with your testMethod will work just fine, as it is in a server side context).

Since you are posting the data to the .aspx page, there is no need to use a web method. You can use Page_Load or OnInit to get the posted data (via the Request page property) and handle the posted data in it.

3 Comments

Thanks, I'm looking into both answers.
So, to get simple feedback from the 'success' function, what I have should work or do I need to implement a page_Load too?
@davy - You don't have to, but you do need to output something on the page. That "something" is your response value, and the HTTP success header (should be 200 if all is well) will indicate success or failure.

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.