WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 4 months ago

#31577 new defect (bug)

wp.ajax.send JS function should send ajax request with dataType json

Reported by: TikiTDO Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.6
Component: General Keywords: has-patch
Focuses: javascript Cc:
PR Number:

Description

Occasionally when using the wp.ajax.send JS function defined in wp-includes/js/wp-util.js:70 the system fails to auto-detect that the response is JSON, even when it is a valid JSON string.

Given that the remainder of the code fails if the response is not a valid object it should be safe to add dataType: 'json' to the default options hash defined on line 78 of that file.

Attachments (1)

wp-util.js.patch (2.8 KB) - added by TikiTDO 5 years ago.

Download all attachments as: .zip

Change History (5)

@TikiTDO
5 years ago

#1 @TikiTDO
5 years ago

  • Keywords has-patch added

#2 follow-up: @nacin
5 years ago

I believe what needs to happen is Content-type: application/json needs to be returned, correct? If you are using the corresponding PHP API (wp_send_json_success()/_error()), then you should always have this content type set.

#3 in reply to: ↑ 2 @TikiTDO
5 years ago

Replying to nacin:

I believe what needs to happen is Content-type: application/json needs to be returned, correct? If you are using the corresponding PHP API (wp_send_json_success()/_error()), then you should always have this content type set.

Logically, yes. However, this was affecting a client when they were trying to add an image to a post, even after I had disabled all plugins. Perhaps there might be a deeper root cause, but I found that the attached fix was sufficient for this scenario.

Given that the function in question only deals with JSON responses I figured it would be a good idea to get this in place, that way even if something goes wrong higher up the chain there is still a chance to recover from it. Otherwise I would like to spare a less experienced developer a painful multi-hour bug hunt.

Last edited 5 years ago by TikiTDO (previous) (diff)

#4 @DrewAPicture
5 years ago

  • Version changed from trunk to 3.6
Note: See TracTickets for help on using tickets.