プロジェクト

全般

プロフィール

Vote #79675

完了

Ajax Request Returns 200 but an error event is fired instead of success

Admin Redmine さんが3年以上前に追加. 3年以上前に更新.

ステータス:
Closed
優先度:
通常
担当者:
-
カテゴリ:
REST API_32
対象バージョン:
開始日:
2022/05/09
期日:
進捗率:

0%

予定工数:
category_id:
32
version_id:
127
issue_org_id:
30073
author_id:
123153
assigned_to_id:
332
comments:
8
status_id:
5
tracker_id:
1
plus1:
0
affected_version:
closed_on:
affected_version_id:
ステータス-->[Closed]

説明

here's an explanation of the issue
https://stackoverflow.com/questions/6186770/ajax-request-returns-200-ok-but-an-error-event-is-fired-instead-of-success

IMO https://github.com/redmine/redmine/blob/dcb545efadc586ab15065870b943763263ed9a46/app/controllers/application_controller.rb#L659 any maybe other places should be changed to return https://httpstatuses.com/204 (no-content) or a valid json {} if the dataType is json

example

    $.ajax({
        type: "DELETE",
        url: '/issues/5.json'
        dataType: 'json'
        success: function (data) {
            alert("ok");
        },
        error: function (textStatus, errorThrown) {
            alert('error');
        }
    });

if fails as an error, because the returned body is blank (not a valid json), but the request was successful

this behaviour is documented
http://api.jquery.com/jquery.ajax/

dataType (default: Intelligent Guess (xml, json, script, or html))
Type: String
The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).

"json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests that have a callback placeholder, e.g. ?callback=?, are performed using JSONP unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

I propose to change

def render_api_ok
  render_api_head :ok
end

to

def render_api_ok
  render_api_head :no_content
end

journals

--------------------------------------------------------------------------------
LGTM. Setting the target version to 3.4.9.
--------------------------------------------------------------------------------
Changed the target version to 4.1.0.

I think we would better not to change the status code in minor versions because it may break some REST API clients which simply assume that the successful status code is "200".
--------------------------------------------------------------------------------
Updated tests.
--------------------------------------------------------------------------------
A comment should be updated: s/200/204/
--------------------------------------------------------------------------------
Committed. Thank you for reporting and fixing this issue.
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------


related_issues

relates,Closed,33975,REST API format: JSON, Update Catelogy response incorrect.

Admin Redmine さんが3年以上前に更新

  • カテゴリREST API_32 にセット
  • 対象バージョン4.1.0_127 にセット

他の形式にエクスポート: Atom PDF

いいね!0
いいね!0