#153 enhancement
nadya

Discrepancy of the documentation and work is possible

Reported by nadya | February 27th, 2010 @ 01:10 PM

In the documentation it is told:
"if the response holds a X-JSON header, its content will be parsed, saved as an object and sent to the callbacks as the second argument:" and there is an example:

new Ajax.Request('/some_url', { method:'get',
  onSuccess: function(transport, json){
      alert(json ? Object.inspect(json) : "no JSON object");
    }
  });

In the work I used such code:


new PeriodicalExecuter(function(pe) {
    new Ajax.Request('updatesWarehouse.php',
              {
                method:'get',
                contentType: 'text/json',
                onSuccess: function(transport,json){
                    alert(json ? Object.inspect(json) : "no JSON object");
                },
                onFailure: function(){ alert('Something went wrong...') }
              });
      }, 5);

The updatesWarehouse.php is designed to send JSON structure:

<?php
header( 'Content-Type: text/json' );
.....

Bur the result is "no JSON object".

The second argument "json" is always empty. Please write to documentation what header I must use to receive the answer in the second argument. How it was planned to use it?
More low in the documentation it is told:

var json = transport.responseText.evalJSON();

Please, explain in the documentation point in which use parametre "json" for answer reception. You describe a unique way to receive in this variable the answer it to fill in with hands.
Many thanks for the place organisation where it is possible so to complain and excuse for weak English.

Comments and changes to this ticket

  • Dan Dean
  • Dan Dean

    Dan Dean February 28th, 2010 @ 02:46 AM

    • Tag changed from ajax, documentation, json to enhancement
    • Assigned user set to “Dan Dean”

    Hi Nadya,

    Generally, for help requests, use the google group http://groups.google.com/group/prototype-scriptaculous or #prototypejs channel on IRC. You're sure to get a quick response in one of those.

    I agree though -- this should be more clearly explained in the docs. I'm going to move this ticket to the documentation project so we can address it there.

    In the meantime, here's how to accomplish what you're looking for:

    Send a header of the name X-JSON and the value being the json data to get evaluated... something like this:

    <?php
    header( "X-JSON: $jsonData" );
    ...
    

    If this header is sent, the value of the header will be provided as the second argument to your callback functions.

  • Tobie Langel

    Tobie Langel February 28th, 2010 @ 11:03 AM

    • State changed from “new” to “open”

    For the record, header JSON as the second argument of the callback has been deprecated. Use the headerJSON property of the response object instead:

    onSuccess: function(transport){
      alert(transport.headerJSON);
    },
    

    However, it seems that your intent is to pass JSON in the body of the response in which case you'll find it's available as the responseJSON property of the response object provided you've set the correct content type ('application/json').

    onSuccess: function(transport){
      alert(transport.responseJSON);
    },
    
  • nadya

    nadya February 28th, 2010 @ 10:06 PM

    Dan Dean and Tobie Langel, many thanks, I do not know as to express how much you have helped.

  • Dan Dean

    Dan Dean March 1st, 2010 @ 12:12 AM

    • State changed from “open” to “enhancement”
    • Tag cleared.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Shared Ticket Bins

People watching this ticket

Pages