drupal_json output reply as text/javascript, while jquery.form.js requires reply to be text/html with around script/data.

So, drupal_json must be changed to

function drupal_json($var = NULL) {
  // We are returning JavaScript, so tell the browser. But, if request was POST and not with XMLHttpRequest, then it 
  // come from jquery.form plugin, and needs to be fixed
  if ($_SERVER["REQUEST_METHOD"]=="POST" && $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest')
    drupal_set_header('Content-type: text/html; charset=utf-8');
  else 
    drupal_set_header('Content-Type: text/javascript; charset=utf-8');

  echo "<textarea>";
  if (isset($var)) {
    echo drupal_to_js($var);
  }
}
CommentFileSizeAuthor
#3 drupal-json-fix.diff658 bytesdatacompboy

Comments

datacompboy’s picture

Sorry, wrong code posted :)
Here is correct one:

function drupal_json($var = NULL) {
  // We are returning JavaScript, so tell the browser. But, if request was POST and not with XMLHttpRequest, then it 
  // come from jquery.form plugin, and needs to be fixed
  if ($_SERVER["REQUEST_METHOD"]=="POST" && $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {
    drupal_set_header('Content-type: text/html; charset=utf-8');
    echo "<textarea>";
  } else 
    drupal_set_header('Content-Type: text/javascript; charset=utf-8');

  if (isset($var)) {
    echo drupal_to_js($var);
  }
}
rfay’s picture

Status: Patch (to be ported) » Active

There was no patch provided, so this can't be set to "patch". Please check to see that your issue has been addressed in D7.

datacompboy’s picture

StatusFileSize
new658 bytes

As for me, "patch" is not needed to replace one function :)
But if you prefer, here is patch attached.

datacompboy’s picture

Status: Active » Patch (to be ported)
rfay’s picture

Status: Patch (to be ported) » Needs review

Thanks, @datacompboy. In Drupal we use patch files for this sort of thing, with few exceptions. When a patch is posted you set the status to "needs review". "Patch to be ported" is when a patch has been applied on one version of Drupal and not on another yet.

I recommend that you check this on Drupal 7, as it will need to go into D7 before it can get into D6, if the issue remains there.

Thanks for your work on this!

Setting to "needs review".

rfay’s picture

@datacompboy, I confirm that the same code exists in D7.

Please do a couple of things for me:

1. Explain the nature of the bug here. It's not always the case that JSON would be in the context of jquery form, right? So might this be the wrong place to solve it? Can you please describe the actual problem that you're having?

2. Please provide a link to the information about text/javascript being wrong. A casual look at the documentation makes it look like they're using text/javascript.

Thanks,
-Randy

rfay’s picture

Issue tags: +rfaynovember

Tagging.

rfay’s picture

Version: 6.x-dev » 7.x-dev
Status: Needs review » Fixed

I'm going to mark this as fixed - reopen if you think otherwise.

I studied the jQuery Form documentation for several minutes without discovering the requirement of using a text/html content type. The example http://jquery.malsup.com/form/#json shows no content type given at all in its response.

And, of course, this would have to be fixed first in D7. drupal_json() has changed and in its new existence is drupal_json_output().

If you want to reopen this, please specify why text/html is important and give a link to the requirement.

And, I should add, please *do* reopen it if it is important.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.