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.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料