douge3492 2016-01-24 13:27
浏览 42

来自REST API的jQuery Post Callback

I am working on an app that will submit data to a REST API and have some questions.

  1. How does jQuery know if my post request was successful or not? Is it only looking at the HTTP status?

  2. Is there a convention on what to return from a POST request to a REST API?

JavaScript

$.post( '/API/removeUser', { Eid: id }, function(data) { row.remove(); } );

PHP SLIM Framework

$app->POST('/API/removeUser', function () use ($app) {
    
    // Get the ID from the jQuery post                  
    $Eid = trim(stripslashes(htmlspecialchars($_POST['Eid'])));         
    
    echo json_encode(removeFunction($Eid));
    
});

</div>
  • 写回答

1条回答

  • dongzhimeng2464 2016-01-24 13:38
    关注

    Your backend should always return the appropriate HTTP status code along with the actual data. 404 for resources that were not found, 403 for unauthorized requests, 200 for successful requests etc. Most AJAX libraries (including jQuery) will rely on those for determining the result of the operation.

    If you need more fine-grained error reporting, you could always include a field like "errorCode" in your response that contains an application-level error code that you define yourself and react to accordingly in your frontend code.

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器