Project

General

Profile

Actions

Defect #30073

closed

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

Added by Pavel Rosický about 5 years ago. Updated almost 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
REST API
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:
Resolution:
Fixed
Affected version:

Description

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


Files

30073.diff (13.5 KB) 30073.diff Go MAEDA, 2019-02-03 12:25
30073-v2.diff (13.8 KB) 30073-v2.diff Go MAEDA, 2019-02-03 15:05

Related issues

Related to Redmine - Defect #33975: REST API format: JSON, Update Catelogy response incorrect.Closed

Actions
Related to Redmine - Defect #37407: REST API Issues.xml call returning 204 no contentClosed

Actions
Actions #1

Updated by Go MAEDA about 5 years ago

  • Target version set to Candidate for next minor release
Actions #2

Updated by Go MAEDA about 5 years ago

  • Target version changed from Candidate for next minor release to 3.4.9

LGTM. Setting the target version to 3.4.9.

Actions #3

Updated by Go MAEDA about 5 years ago

  • Target version changed from 3.4.9 to 4.1.0

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".

Actions #4

Updated by Go MAEDA about 5 years ago

Updated tests.

Actions #5

Updated by Go MAEDA about 5 years ago

A comment should be updated: s/200/204/

Actions #6

Updated by Go MAEDA about 5 years ago

  • Status changed from New to Closed
  • Resolution set to Fixed

Committed. Thank you for reporting and fixing this issue.

Actions #7

Updated by Go MAEDA almost 5 years ago

  • Assignee set to Go MAEDA
Actions #8

Updated by Go MAEDA over 3 years ago

  • Related to Defect #33975: REST API format: JSON, Update Catelogy response incorrect. added
Actions #9

Updated by Holger Just over 1 year ago

  • Related to Defect #37407: REST API Issues.xml call returning 204 no content added
Actions

Also available in: Atom PDF