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.

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿