3

fetch data from server returns me json data as a string datatype rather than as application/json datatype, as a result the collection does not get refreshed.

I have tried giving the jquery.ajax option contentType:"application/json" to the fetch options, but still does not work.

how can i make it work? do i send a mimetype from the server? if so, how?

i am using json_encode on the data sent.

preloader.fetch({
        contentType:'application/json'
    });

preloader is an instance of my collection.

edit: my template for a subview was not getting detected as i had kept it out of the masterview's $el element, corrected it, and now i am getting underscore.js error, that

str is null in

str.replace(/\\/g, '\\\\')     //at line 913

is this because the backbone app is not taking it as a json object?

Request headers

Connection  close
Content-Type    text/html
Date    Thu, 12 Apr 2012 13:00:58 GMT
Server  Apache
Transfer-Encoding   chunked
Vary    Accept-Encoding

Response headers has the line

Accept  application/json, text/javascript, */*; q=0.01

means it is a json, then what is the problem?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • possible duplicate of [add request header on backbone](http://stackoverflow.com/questions/10081728/add-request-header-on-backbone) – tkone Apr 12 '12 at 12:27
  • its not the same, that guy wants to send data before making a fetch() request, i want to read the received data as a json. – Vipinesh Singh Apr 12 '12 at 12:31
  • I think you have a problem in the server side. It's not only the _contenttype_ but something else. Can you show us some how how the _request_ and the _response_ look like? for example: `$ curl http://localhost/myresource` and `$ curl -I http://localhost/myresource`. – fguillen Apr 12 '12 at 12:57
  • Also.. don't add new questions to your already existing one. Focus in a concrete problem. – fguillen Apr 12 '12 at 13:00
  • i just saw the response and request headers, everything looks alright there, – Vipinesh Singh Apr 12 '12 at 13:02
  • i am new to stackoverflow, i apologize if my format is not correct – Vipinesh Singh Apr 12 '12 at 13:06
  • @VipineshSingh it is the same -- how do you modify an ajax request that backbone makes. The `$.ajax` method in jQuery can be told what datatype to coerce a response into. Using `$.ajaxSetup` will allow you to set that parameter, and force the `$.ajax` command to coerce the response into a JSON, regardless of what your server is saying the data type is. [dataType section on $.ajax](http://api.jquery.com/jQuery.ajax/) – tkone Apr 12 '12 at 16:16
  • @VipineshSingh you have shown us the headers of the response.. but what about the **content of the response**. Is there there a proper JSON content?. Can you show us it somehow? – fguillen Apr 13 '12 at 07:49

1 Answers1

5

I think the contentType option is for the request (your request).

Try dataType:"json".

fguillen
  • 36,125
  • 23
  • 149
  • 210
  • 1
    this is not necessary if you set proper headers for the server output "Content-Type: application/json" – opengrid Apr 12 '12 at 13:49