0

Here's my request and the response from my server:

Request Headers

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:512
Content-Type:application/json; charset=UTF-8
DNT:1
Host:192.168.59.103:8080
Origin:http://192.168.59.103:9000
Referer:http://192.168.59.103:9000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36

Request Payload

{"notification":{"status":"testing","date_opened":null,"time_opened":null,"date_closed":null,"time_closed":null,"subsystem_affected":null,"who_resolved_it":null,"who_sent_it":null,"reason":null,"impact":null,"resolution":null,"description_of_problem":"testing","downtime_date_begin":null,"downtime_time_begin":null,"number_of_days":null,"number_of_hours":null,"number_of_minutes":null,"downtime_date_end":null,"downtime_time_end":null,"notification_number":null,"begin_user":null,"end_user":null,"subject":null}}

Response Headers

Remote Address:192.168.59.103:8080
Request URL:http://192.168.59.103:8080/api/notifications
Request Method:POST
Status Code:201 Created


Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, x-requested-with, Accept
Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin:*
Content-Length:0
Content-Type:text/plain; charset=utf-8
Date:Tue, 07 Oct 2014 19:58:39 GMT
Location:http://192.168.59.103:8080/api/notifications/11
ConsoleSearchEmulationRendering

Here's the function that I'm calling if saving the new record fails:

function failure(err) {
  console.log("Oops!");
  console.log(err);
}

This outputs an object on the console, but I can't figure out what the problem is. As you can see from the server response above, the headers and HTTP status code seem to be correct per http://jsonapi.org/format/ .

And here's the line where I try to save the new "notification" model:

notification.save().then(afterSaving, failure);

The err object looks like this on the console:

Oops! combined-scripts.js:206 Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}abort: function ( statusText ) {always: function () {complete: function () {done: function () {error: function () {fail: function () {getAllResponseHeaders: function () {getResponseHeader: function ( key ) {overrideMimeType: function ( type ) {pipe: function ( /* fnDone, fnFail, fnProgress */ ) {progress: function () {promise: function ( obj ) {readyState: 4responseText: ""setRequestHeader: function ( name, value ) {state: function () {status: 201statusCode: function ( map ) {statusText: "Created"success: function () {then: null__proto__: Object

hourback
  • 1,150
  • 4
  • 12
  • 27

2 Answers2

1

You have put two different sets of response headers. Which one is correct?

The second looks like you're responding with a redirect with the location header to the newly created resource. I'm not sure if ember can handle that, it expects that the server returns the created object.

Jakeii
  • 1,273
  • 9
  • 16
  • Ah-ha! I will try returning the created object. I didn't know that was a requirement. I copied and pasted the response and request from Chrome and I think the headers from a single response are simply split up into two blocks. I'll consolidate them in my original post. – hourback Oct 08 '14 at 14:30
  • where do you see that Ember expects that the server will return the created object? Sorry -- I haven't been able to find that detail in the documentation. I'd just like to know so I can find out what else I might be omitting from other server responses. – hourback Oct 08 '14 at 14:36
  • 1
    Looks like that fixed it! I returned the new, created object to the client and no more error. – hourback Oct 08 '14 at 15:43
  • 1
    I'm not sure where I picked that up unfortuneatly! But you can see [here](http://stackoverflow.com/questions/14922623/what-is-the-complete-list-of-expected-json-responses-for-ds-restadapter) a compilation of the expected responses, the rule seems to be always return the object(s) unless deleteing, in that case return an empty object `{}` – Jakeii Oct 08 '14 at 15:48
1

I had similar problem, but with Ember.$.post (it's not RESTAdapter and it shouldn't expect REST response with {'user' : {...}}

Ember.$.post(url, data)
.then(
 function(resp) { console.log("OK"); }, 
 function(reason) { console.log("FAILED"); }
);

After all I got status 201 (it's OK) and FAILED.

The problem was header: expected application/json but got emtpy string.