JEditable,如何处理JSON响应? [英] JEditable, how to handle a JSON response?
问题描述
现在,我正在使用的服务器响应会发回这样的JSON响应:
Right now, the server response I'm working with sends back a JSON response like this:
{"status":1}
保存后,可编辑的实际响应:{"status":1}
放置在页面上.无论如何要解决这个问题?
After saving, jeditable places the actual response: {"status":1}
on the page. Anyway to get around this issue?
推荐答案
更好的解决方案是在返回的json数据到达页面之前对其进行后处理.
A better solution is to post-process the returned json data before it hits the page.
假设您的服务器返回以下json字符串:
Suppose your server returns the following json string:
{ "status": 1, "result": "value to be displayed", "other": "some other data" }
,您想处理状态"和其他"字段,并在可编辑输入字段中显示结果"字段.
and you would like to process the "status" and "other" fields, and display the "result" field in the jeditable input field.
将以下两行添加到jquery.jeditable.js:
Add the following 2 lines to jquery.jeditable.js:
(第95行附近):
var intercept = settings.intercept || function(s) {return s; };
(在第350行附近,成功:功能(结果,状态){"之后
(around line 350, right after " success : function(result, status) {"
result = intercept.apply(self,[result]);
然后,用您自己的代码执行以下操作:
Then, in your own code, do something like the following:
$(some_field).editable(
'/some_url_on_your_server',
{
indicator : "<img src='/images/spinner.gif'>",
tooltip: "Click to edit.",
indicator: "Saving...",
onblur: "submit",
intercept: function (jsondata) {
obj = jQuery.parseJSON(jsondata);
// do something with obj.status and obj.other
return(obj.result);
},
etc.
这使您可以做一些很酷的事情,例如让服务器将缩写词转换为完整字符串等.
This allows you do cool stuff like having your server convert abbreviations to full strings etc.
享受!
这篇关于JEditable,如何处理JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!