Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Backbone expects JSON as the default response format. <strike>When you get an integer, i suspect, backbone is using jquery <code>$.parseJSON()</code> on the number and returns it as valid JSON while it is not.</strike> If you want to return multiple errors messages, i suggest you put them into separate fields of an array and encode them then send as response to backbone.</p> <h3>EDIT</h3> <p>Just checked backbone source code and it doesn't call on <code>$.parseJOSN()</code> contrary to what was guessed above.</p> <h3>EDIT 2</h3> <p>Assume you have the following PHP code (as much as I would like to create a framework agnostic example, it is possible but things will be quicker and smoother using a framework so I have picked <a href="http://www.slimframework.com/" rel="nofollow">Slim</a>).</p> <p>When you save a model, backbone sends the data to the server using POST as the method. In Slim this will translate into the following:</p> <pre><code>$app = new \Slim\Slim(); $app-&gt;post('/authors/', function() use($app) { // We get the request data from backbone in $request $request = $app-&gt;request()-&gt;getBody(); // We decode them into a PHP object $requestData = json_decode($request); // We put the response object in $response : this will allow us to set the response data like header values, etc $response = $app-&gt;response(); // You do all the awesome stuff here and get an array containing data in $data // // Sample $data in case of success $data = array( 'code' =&gt; 200, 'status' =&gt; 'OK', 'data' =&gt; array('id'=&gt;1, 'name' =&gt; 'John Doe') ); // sample $data in case of error $data = array( 'code' =&gt; 500, 'status' =&gt; 'Internal Server Error', 'message' =&gt; 'We were unable to reach the data server, please try again later' ); // Then you set the content type $app-&gt;contentType('application/json'); // Don't forget to add the HTTP code, Backbone.js will call "success" only if it has 2xx HTTP code $app-&gt;response()-&gt;status( $data['code']); // And finally send the data $response-&gt;write($data); </code></pre> <p>I have used Slim simply because it gets the job done and everything should read like English.</p> <p>As you can see, Backbone will need the response in JSON format. I just tested on one of my websites, if you send an HTTP code different from 2xx, Backbone will actually call <code>error</code> instead of <code>success</code>. But the browser will also catch that HTTP code, so watch out!</p> <p><strong>REMOVED INFO</strong></p> <p>Backbone <code>parse</code> function will be expecting JSON as well! Assume we have this collection:</p> <pre><code>var AuthorsCollection = Backbone.Collection.extend({ initialize: function( models, options ) { this.batch = options.batch }, url: function() { return '/authors/' + this.batch; }, // Parse here allows us to play with the response as long as "response" is a JSON object. Otherwise, Backbone will automatically call the "error" function on whatever, say view, is using this collection. parse: function( response ) { return response.data; } }); </code></pre> <p><code>parse</code> allows you to inspect the response, but will not accept anything else than JSON!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload