如何从 PHP 创建 jqGrid JSON 错误响应,然后使用 jqGrid javascript 响应它?

发布于 2024-12-05 11:36:18 字数 202 浏览 0 评论 0原文

我一直在搜索互联网和 jqGrid 文档,但我找不到任何有关创建、更新、删除 (CRUD) 操作的 JSON 响应格式的信息。当然应该有一个从 PHP 返回到 jqGrid 的 JSON 消息来告诉它 CRUD 操作是否成功?该消息的格式是什么?您将如何编写 jqGrid 的 javascript 来响应该消息?我不是一个很好的程序员,所以完整的代码答案将不胜感激。

谢谢

I have been searching the internet and the jqGrid documentation, but I can't find anything about the format of a JSON response to a create, update, delete (CRUD) operation. Surely there should be a JSON message that is returned from PHP to jqGrid to tell it whether the CRUD operation was successfull? What is the format of this message and how would you code the javascript for the jqGrid to respond to that message? I'm not a very good programmer, so complete code answers would be greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一萌ing 2024-12-12 11:36:18

您不需要使用响应来创建、删除和更新。

例如,如果您执行创建操作,您将调用“ajax 操作”,它将您的数据添加到数据库中。

现在有两种可能性:

  1. 创建操作成功
    • 不返回任何内容,表示空字符串
    • (只要jqgrid收到200响应,就一切正常)
  2. 创建操作失败
    • 只需抛出带有修改后的响应标头的异常

如果 jqGrid 收到非 200 响应代码,它本身会向您显示错误!

try {
    // insert something in your db
    // ok = true means everything fine
    // ok = false means something unpredictable happened
    if (!$ok) {
       throw new Exception('error');
    }

} catch (Exception $e) {

    header("Status: 500 Server Error caused by dbinsert jqgrid");
    var_dump($e->getMessage());
}

对代码感到抱歉,但这是我现在脑子里最快的:)
我将 jqGrid 与 Zend Framework 结合使用,ZF 默认使用 500 个异常响应代码(至少是我的模板)。

成功更新/删除/创建后,您必须重新获取整个 jqGrid 数据。

jQuery("#your_jqgrid_id").jqGrid().trigger('reloadGrid');

据我所知没有其他机制。 (大约 6 个月前使用过它,也许情况发生了变化)

如果您想实现自己的错误/成功处理,只需以您想要的任何格式定义您自己的消息,并在 ajax 成功函数回调中处理它。

如果您需要更多代码并且不紧急,请给我留言。

另一条建议:不要指望立即理解 jqGrid。花点时间,尝试一些事情,玩玩它。您需要一段时间才能适应它。

You don't need to use a response for create, delete and update.

E.g. if you do an create operation you are calling an "ajax operation" which adds your data into a database.

There are now two possibilites:

  1. Create Operation succeeds
    • just return nothing, means empty string
    • (as long as a 200 response is received by jqgrid, everything is fine)
  2. Create Operation failed
    • just throw an Exception with a modified response header

If jqGrid receives an non 200 response code it shows you an error itself!

try {
    // insert something in your db
    // ok = true means everything fine
    // ok = false means something unpredictable happened
    if (!$ok) {
       throw new Exception('error');
    }

} catch (Exception $e) {

    header("Status: 500 Server Error caused by dbinsert jqgrid");
    var_dump($e->getMessage());
}

Sorry for the code, but it was the fastest I get out of my brain now :)
I use jqGrid in combination with the Zend Framework and ZF uses 500 Response codes for Exceptions by default (at least my template)

After a successful update/delete/create you have to refetch the whole jqGrid data.

jQuery("#your_jqgrid_id").jqGrid().trigger('reloadGrid');

There is afaik no other mechanism. (Used it last about 6 months ago, maybe that changed)

If you want to implement your own error/success handling, just define your own message in whatever format you want and handle it in the ajax success function callback.

If you need more code and it is not urgent, just drop me a comment.

One additional advice: Don't expect to understand jqGrid immediatly. Take your time, try some things, play with it. It will take some time before you feeling comfortable with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文