0

我正在尝试使用 JSON、AJAX 和 JQUERY 从 httphandler 文件向客户端发送两条连续消息。但是,仅显示第二条消息。可能是什么原因?你如何显示这两条消息?

.ASHX

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Converters;

public class Gen_BLL : IHttpHandler {
  public void ProcessRequest (HttpContext context) {
string ename = JsonConvert.SerializeObject("Message1..");
context.Response.Clear();
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Write(ename);

ename = JsonConvert.SerializeObject("Message2..");
context.Response.Clear();
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Write(ename);

}

public bool IsReusable {
get {return false;}

}

}

.JS 文件:

function funGenAst(usro){
 $.ajax({
type: "GET",    
url: "Gen_BLL.ashx",
contentType: "application/json; charset=utf-8",
data: { "uso": usro },
dataType: "json",
success: OnComplete,        
error: OnFail
});
 return false;
}

function OnComplete(result) {
  alert(result);
}

function OnFail(result){
  alert("fail...');
}

只显示一个警报,即“Message2..”。“Message1..”警报根本没有弹出。您如何一个接一个地提醒这两条消息?

谢谢 bsc

4

1 回答 1

0

You simple can not do that.

There is a protocol of http communication, and this is break it, from the moment you send some header and some content, there is not prediction to clear the header, and send a second (or new) message.

About Hypertext Transfer Protocol (HTTP): http://www.w3.org/Protocols/rfc2616/rfc2616-sec1.html

Alternative way: Merge your message to one response, or after you receive the first message, make new request to get the second one.

于 2012-06-20T09:50:04.460 回答