jQuery的POST到回来.NET的HttpListener和 [英] jQuery POST to a .NET HttpListener and back again

查看:319
本文介绍了jQuery的POST到回来.NET的HttpListener和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用jQuery.post发送一些数据到一个.NET应用程序,是使用的HttpListener



JavaScript代码的一大块

这里的JS:

  $后(HTTP://本地主机:8080 /捕获,{名字:约翰,时间:下午2点 } 
功能(数据){
警报(数据);
});

和C#的:

  HttpListenerContext背景= listener.GetContext(); 
HttpListenerRequest请求= context.Request;

StreamReader的读者=新的StreamReader(request.InputStream);
字符串s2 = reader.ReadToEnd();
Console.WriteLine(收到的数据:+ S2);

//获取响应对象。
HttpListenerResponse响应= context.Response;
//构造一个响应。
串responseString =< HTML>< BODY>您好!世界< / BODY>< / HTML>中;
字节[]缓冲= System.Text.Encoding.UTF8.GetBytes(responseString);
//获取响应流和写入的响应。
response.ContentLength64 = buffer.Length;
的System.IO.Stream输出= response.OutputStream;
output.Write(缓冲液,0,buffer.Length);
//你必须关闭输出流。
output.Close();



POST请求熄灭OK,和.NET应用程序中的数据读取正常,但JS代码似乎并没有得到响应。回调函数的jQuery.post火灾,但数据总是undefined.For简洁我省略了一些C#上面,我设定的前缀为听者。



任何想法,为什么我没有收到我的数据备份客户端



编辑:我要补充一点,当我同是HttpFox运行JS运行我得到HTTP代码200,'NS_ERROR_DOM_BAD_URI',我认为有一些东西需要做的的http://本地主机:8080 /捕获我瞄准,但是当我打的资源在Firefox中,我。获取HTML响应就好了,它注册为GET,200



编辑:我简化了回应只是'喵喵',和这是小提琴手是给我的全响应:

  HTTP / 1.1 200 OK 
的Content-Length:4
的Content-Type:text / html的
服务器:HTTPAPI / 2.0
日期:星期五,2011年4月15日格林威治时间十二点58分49秒



解决方案

不要忘了的同根同源策略限制。除非你的JavaScript是在 HTTP托管://本地主机:8080 你不会要能够AJAX请求发送到这个网址。不同的端口号也不会被允许的。您需要将接待来自 HTTP服务的HTML页面上的JavaScript文件://本地主机:8080 如果你想要这个工作。或者你的服务器发送的 JSONP 但这仅适用于GET请求



备注:请正确通过包装处理在服务器上一次性资源他们 using语句或您的服务器可能会开始漏水网络连接手柄。


I have a chunk of javascript code that uses jQuery.post to send some data to a .NET app that's using an HttpListener.

Here's the js:

$.post("http://localhost:8080/catch", { name: "John", time: "2pm" },
    function(data) { 
        alert(data);
    });

and the 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();

The post request goes out ok, and the .NET app reads in the data ok, but the JS code doesn't seem to get the response. The callback function to the jQuery.post fires, but data is always undefined.For brevity I have omitted some C# above where I set the prefixes to the listener.

Any ideas why I'm not getting my data back client-side?

EDIT: I should add that when I run the JS with HttpFox running I get Http code 200, 'NS_ERROR_DOM_BAD_URI', which I thought had something to do with the "http://localhost:8080/catch" I was targeting, but when I hit that resource in firefox, i get the HTML response just fine and it registers as a GET, 200.

EDIT: I simplified the response to just 'meow', and this is what fiddler is giving me for the full response:

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

解决方案

Don't forget about the same origin policy restriction. Unless your javascript is hosted on http://localhost:8080 you won't to be able to send AJAX requests to this URL. A different port number is not allowed either. You will need to host your javascript file on an HTML page served from http://localhost:8080 if you want this to work. Or have your server send JSONP but this works only with GET requests.

Remark: make sure you properly dispose disposable resource on your server by wrapping them in using statements or your server might start leaking network connection handles.

这篇关于jQuery的POST到回来.NET的HttpListener和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆