2011-04-26 38 views
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); 
    } 

在我的ASP.NET应用程序中,我正在从page_load事件读取流,但没有收到数据。我究竟做错了什么?不接收来自http输入流的数据

C#代码在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(); 

     } 
... 

回答

1

它看起来像这篇文章包含你正在尝试做的精确代码:

Fake a form submission with C# WebClient

如果你只需要数据在Page_Load中,不需要使用JavaScript来做到这一点 - 对吗?

我个人不使用XMLHttpRequest对象了。我放弃了它,倾向于使用jQuery AJAX函数。成功发布的回调函数可以很容易地捕获服务器的响应。

下面是如何用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); 
    } 
} 
); 
+0

我不实际需要发送数据时,它只是一个试验。我试图做的就是发送一个请求,从我的Firefox,插件(我的asp.net应用程序),当点击一个按钮,查看建议。您给我的帖子涉及发送帖子数据,没有收到帖子,或者我误解了? – michelle 2011-04-26 15:47:28

+0

它兼具两种功能。首先发送数据,然后收到响应。如果您正试图通过Firebug查看AJAX交通,确保你去XHR选项卡。 – 2011-04-26 15:54:01

+0

提琴手有滚动您自己的请求,发送到服务器,然后看到什么回来优秀的能力。你可能想尝试使用它。 http://www.fiddler2.com/fiddler2/ – 2011-04-26 16:00:28