dongwei7048 2011-09-19 15:24 采纳率: 100%
浏览 47
已采纳

如何从PHP创建jqGrid JSON错误响应,然后使用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

  • 写回答

1条回答 默认 最新

  • dongxun7962 2011-09-19 17:42
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀