【发布时间】:2011-08-05 17:51:55
【问题描述】:
我有一段 javascript 代码使用 jQuery.post 将一些数据发送到使用 HttpListener 的 .NET 应用程序。
这是 js:
$.post("http://localhost:8080/catch", { name: "John", time: "2pm" },
function(data) {
alert(data);
});
和 C#:
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
StreamReader reader = new StreamReader(request.InputStream);
string s2 = reader.ReadToEnd();
Console.WriteLine("Data received:" + s2);
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
发布请求正常,.NET 应用程序读取数据正常,但 JS 代码似乎没有得到响应。触发 jQuery.post 的回调函数,但 data 始终未定义。为简洁起见,我省略了上面设置监听器前缀的一些 C#。
知道为什么我没有在客户端取回我的数据吗?
编辑:我应该补充一点,当我在运行 HttpFox 的情况下运行 JS 时,我得到 Http 代码 200,'NS_ERROR_DOM_BAD_URI',我认为这与“http://localhost: 8080/catch”我的目标是,但是当我在 Firefox 中点击该资源时,我得到的 HTML 响应很好,它注册为 GET,200。
编辑:我将响应简化为“喵”,这就是提琴手给我的完整响应:
HTTP/1.1 200 OK
Content-Length: 4
Content-Type: text/html
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 15 Apr 2011 12:58:49 GMT
meow
【问题讨论】:
标签: c# javascript http