问题描述
在努力使进度报告过程多了几分可靠和请求/响应解耦了,我在执行Windows服务的处理,并坚持到一个文件中预期的反应。当客户端启动更新查询,其用意是控制器返回文件的内容,无论他们是作为一个JSON字符串。
该文件的内容是pre序列化到JSON。这是为了确保有在响应的方式没有什么地位。没有处理需要做的(短读取文件内容到一个字符串并将其返回)获得响应。
我最初虽然这将是相当简单的,但它不转动到这样的情况
目前我的控制器方法看起来正是如此:
控制器
更新
[HttpPost]
公共JsonResult UpdateBatchSearchMembers()
{
字符串路径= Properties.Settings.Default.ResponsePath;
串returntext;
如果(!System.IO.File.Exists(路径))
returntext = Properties.Settings.Default.EmptyBatchSearchUpdate;
其他
returntext = System.IO.File.ReadAllText(路径); 返回this.Json(returntext);
}
和提琴手正在恢复它作为原料的响应
HTTP / 1.1 200 OK
服务器:ASP.NET开发服务器/ 10.0.0.0
日期:星期一,2012年3月19日20点30分05秒GMT
的X ASPNET-版本:4.0.30319
的X AspNetMvc-版本:3.0
缓存控制:私人
内容类型:应用程序/ JSON的;字符集= UTF-8
内容长度:81
连接:关闭{\\StopPolling \\:假的,\\BatchSearchProgressReports \\:[],\\MemberStatuses \\:[]}
AJAX
更新
下面将可能在以后更改,但现在这个时候,我产生了响应类是工作并返回它作为JSON像一个正常的人。
this.CheckForUpdate =功能(){
VAR父=这一点;如果(this.BatchSearchId = NULL&放大器;!&安培;!WorkflowState.SelectedSearchList =){
showAjaxLoader = FALSE;
如果(progressPending!=真){
progressPending = TRUE;
$阿贾克斯({
网址:SearchListID =WorkflowState.UpdateBatchLink + + WorkflowState.SelectedSearchList,
输入:POST,
的contentType:应用/ JSON的;字符集= UTF-8,
缓存:假的,
成功:功能(数据){
对于(VAR I = 0; I< data.MemberStatuses.length;我++){
VAR响应= data.MemberStatuses [I]
parent.UpdateCellStatus(响应);
}
如果(data.StopPolling =真){
parent.StopPullingForUpdates();
}
showAjaxLoader = TRUE;
}
});
progressPending = FALSE;
}
}
的问题,我认为,这是JSON动作的结果是为了拿一个对象(模型),并创建与内容作为JSON- HTTP响应从你的模型对象格式的数据。
你所传递给控制器的JSON的方法,虽然是一个JSON格式的 string对象的,所以它是序列化的字符串对象为JSON,这就是为什么HTTP内容响应由双引号(我假定这就是问题所在)所包围。
我觉得你可以考虑使用内容的行动结果来替代JSON动作的结果,因为你实际上已经有可用的HTTP响应的原始内容。
返回this.Content(returntext,应用/ JSON);
//不知道离手,如果你还应该指定的charset = UTF-8在这里,
//或者如果是自动完成的
另外,也可以从服务反序列化JSON结果到一个对象,然后传递对象到控制器的JSON的方法,但缺点有,你将被去串行化,然后重新序列化的数据,这可能没有必要为您的目的。
In an effort to make a progress reporting process a little more reliable and decouple it from the request/response, I am performing the processing in a Windows Service and persisting the intended response to a file. When the client starts polling for updates, the intention is that the controller returns the contents of the file, whatever they are, as a JSON string.
The contents of the file are pre-serialized to JSON. This is to ensure that there is nothing standing in the way of the response. No processing needs to happen (short of reading the file contents into a string and returning it) to get the response.
I initially though this would be fairly simple, but it is not turning out to be the case.
Currently my controller method looks thusly:
Controller
Updated
[HttpPost]
public JsonResult UpdateBatchSearchMembers()
{
string path = Properties.Settings.Default.ResponsePath;
string returntext;
if (!System.IO.File.Exists(path))
returntext = Properties.Settings.Default.EmptyBatchSearchUpdate;
else
returntext = System.IO.File.ReadAllText(path);
return this.Json(returntext);
}
And Fiddler is returning this as the raw response
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 19 Mar 2012 20:30:05 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 3.0
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 81
Connection: Close
"{\"StopPolling\":false,\"BatchSearchProgressReports\":[],\"MemberStatuses\":[]}"
AJAX
Updated
The following will likely be changed later, but for now this was working when I was generating the response class and returning it as JSON like a normal person.
this.CheckForUpdate = function () {
var parent = this;
if (this.BatchSearchId != null && WorkflowState.SelectedSearchList != "") {
showAjaxLoader = false;
if (progressPending != true) {
progressPending = true;
$.ajax({
url: WorkflowState.UpdateBatchLink + "?SearchListID=" + WorkflowState.SelectedSearchList,
type: 'POST',
contentType: 'application/json; charset=utf-8',
cache: false,
success: function (data) {
for (var i = 0; i < data.MemberStatuses.length; i++) {
var response = data.MemberStatuses[i];
parent.UpdateCellStatus(response);
}
if (data.StopPolling = true) {
parent.StopPullingForUpdates();
}
showAjaxLoader = true;
}
});
progressPending = false;
}
}
The issue, I believe, is that the Json action result is intended to take an object (your model) and create an HTTP response with content as the JSON-formatted data from your model object.
What you are passing to the controller's Json method, though, is a JSON-formatted string object, so it is "serializing" the string object to JSON, which is why the content of the HTTP response is surrounded by double-quotes (I'm assuming that is the problem).
I think you can look into using the Content action result as an alternative to the Json action result, since you essentially already have the raw content for the HTTP response available.
return this.Content(returntext, "application/json");
// not sure off-hand if you should also specify "charset=utf-8" here,
// or if that is done automatically
Another alternative would be to deserialize the JSON result from the service into an object and then pass that object to the controller's Json method, but the disadvantage there is that you would be de-serializing and then re-serializing the data, which may be unnecessary for your purposes.
这篇关于MVC:如何返回一个字符串作为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!