0
function sendRequestToDelicious()
{
var xmlhttp=false;
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    try {
        xmlhttp = new XMLHttpRequest();

    } catch (e) {
        xmlhttp=false;
    }
    }
    if (!xmlhttp && window.createRequest) {
    try {
        xmlhttp = window.createRequest();
    } catch (e) {
        xmlhttp=false;
    }
    }


  var url = "http://localhost:52271/WebForm1.aspx";
var params = "q=hello";
xmlhttp.open("POST", url, true);

//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);

xmlhttp.send(params);
    }

In my ASP.NET app, I am reading the stream from page_load event, but I'm not receiving the data. what am I doing wrong?

C# CODE IN ASP.NET:

    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection conn;

        protected void Page_Load(object sender, EventArgs e)
        {

            StreamReader reader = new StreamReader(Page.Request.InputStream);
            String data = reader.ReadToEnd();

        }
...
michelle
  • 2,759
  • 4
  • 31
  • 46

1 Answers1

1

It looks like this post contains the precise code for what you are trying to do:

Fake a form submission with C# WebClient

If you just need the data at Page_Load there isn't a requirement to do this with JavaScript- right?

I personally don't use the XmlHttpRequest object any more. I have abandoned it in favor of using the jQuery AJAX functions. The callback function for a successful post would make it easy to capture the response from the server.

Here is an example of how to do it with jQuery AJAX:

$.ajax(
{
    type : 'POST',
    url : 'http://localhost:52271/WebForm1.aspx',
    dataType : 'json',
    data: 
    {
        q:'hello'
    },
    success : function(data)
    {        
        $('mydiv').text(data.msg).show(500);     
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) 
    {
        $('mydiv').text('There was an error.').show(500);
    }
}
);
Community
  • 1
  • 1
Trey Carroll
  • 2,382
  • 4
  • 22
  • 28
  • I dont actually need to send data, it's just a test. what I am trying to do is send a request from my firefox-addon (to my asp.net app) when it clicks on a button, to view recommendations. the post you gave me deals with sending post data, not receiving it, or am I mistaken? – michelle Apr 26 '11 at 15:47
  • It does both. First it sends the data and then it receives the response. If you are trying to watch ajax traffic via Firebug, make sure that you go to the XHR tab. – Trey Carroll Apr 26 '11 at 15:54
  • Fiddler has excellent capabilities for rolling your own requests, sending to a server, and then seeing what comes back. You might want to try using it. http://www.fiddler2.com/fiddler2/ – Trey Carroll Apr 26 '11 at 16:00
  • I've given Fiddler a try, you are right, its a good way to see what data is coming in and going out. However, I don't know where I should place the code in asp.net, is there some sort of HTTPWebRequest event? – michelle Apr 26 '11 at 16:39
  • 1
    I would just create an HTML page with the script above wrapped in a $(document).ready( ...); + download and include jQuery in a Script tag. Then when the DOM elements are loaded the script will fire and post to your aspx page. After that, you will see the HTML from that page comeback as the response. If that's not what you want, please provide more details. – Trey Carroll Apr 26 '11 at 16:50