WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 3 years ago

#30638 closed defect (bug) (invalid)

Missing dataType in Customizer Widgets

Reported by: iseqqavoq Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.0.1
Component: Customize Keywords: reporter-feedback
Focuses: javascript Cc:

Description

In wp-admin/js/customize-widgets.js line 980 the dataType parameter is missing for jQuery post method.
This results in return value getting read as a string instead of json object.

Scenario: User edits widget via Customize panel, an error gets shown.

Suggestion:

change

jqxhr = $.post( wp.ajax.settings.url, data );

into

jqxhr = $.post( wp.ajax.settings.url, data, function(){}, 'json' );

Attachments (1)

30638.diff (572 bytes) - added by westonruter 6 years ago.
Explicitly specify json as the dataType for update-widget Ajax requests

Download all attachments as: .zip

Change History (8)

#1 @westonruter
6 years ago

  • Focuses javascript added
  • Keywords reporter-feedback added

Thanks for the suggestion, iseqqavoq.

Is this an actual problem that you have encountered?

When no dataType is supplied, then jQuery says that the default behavior is to do an “Intelligent Guess (xml, json, script, text, html).” So it should be detecting that the response is JSON and parsing it as such. If this is not the case for you, perhaps there is a PHP error that is getting output first and causing HTML to get injected before the JSON content starts, and in this case the response would not be interpreted as JSON. But it wouldn't really matter in that case, because it couldn't be parsed as JSON anyway.

#2 @iseqqavoq
6 years ago

The output from the ajax request is correct, but gets handled as a string instead of a JSON object.
Debugging has been carried out to identify possible PHP errors, warnings or notices.

#3 @westonruter
6 years ago

Well I cannot reproduce the problem. If I go into the Customizer and edit a widget, an Ajax request gets kicked off with the request headers including:

POST /wp-admin/admin-ajax.php HTTP/1.1
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
...

And the response headers (note the Content-Type):

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 13 Dec 2014 03:18:20 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.4
Access-Control-Allow-Origin: http://src.wordpress-develop.dev
Access-Control-Allow-Credentials: true
X-Robots-Tag: noindex
X-Content-Type-Options: nosniff
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
X-Frame-Options: SAMEORIGIN

The response body is a well-formed JSON document:

{"success":true,"data":{"form":"\t\t<p><label for=\"widget-calendar-25-title\">Title:<\/label>\n\t\t<input class=\"widefat\" id=\"widget-calendar-25-title\" name=\"widget-calendar[25][title]\" type=\"text\" value=\"test!\" \/><\/p>\n","instance":{"encoded_serialized_instance":"...","title":"test!","is_widget_customizer_js_value":true,"instance_hash_key":"..."}}}

And if patch customize-widgets.js as follows:

@@ -1017,6 +1017,7 @@
                        jqxhr = $.post( wp.ajax.settings.url, data );
 
                        jqxhr.done( function( r ) {
+                               console.info( typeof r, r );
                                var message, sanitizedForm,     $sanitizedInputs, hasSameInputsInResponse,
                                        isLiveUpdateAborted = false;

Then I see in my console that r is an object and it is properly parsed.

Please share the same details about your environment so we can identify where the problem lies.

#4 @westonruter
6 years ago

Also, more importantly, what plugins do you have activated? Is a plugin overriding jQuery with an old version? The jQuery docs on dataType show:

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

So if a plugin is forcing a version of jQuery prior to 1.4 then this could be the problem. And the plugin would be doing a bad thing.

@westonruter
6 years ago

Explicitly specify json as the dataType for update-widget Ajax requests

#5 @westonruter
6 years ago

Related: r31409

We should be using wp.ajax.post() instead of jQuery.post anyway.

This ticket was mentioned in Slack in #core-customize by westonruter. View the logs.


3 years ago

#7 @westonruter
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Closing due to lack of reporter-feedback and inability to reproduce.

Note: See TracTickets for help on using tickets.