Ember Data save response error

Hej guys, i have the following problem. i am doing this:

saveData() {
      const myStore = this.get('store');
      let post = myStore.createRecord('instantiatedwidget', {
        userID: 1995,
        name: 'test',
        instanceID: 404
      });

      post.save();
    }

my backend get the post request and the data. after that i return in java this

return Response.status(200).entity("hello world").build();

(jersey framework for rest api)

now my ember application throws that error:

error SyntaxError: Unexpected token P in JSON at position 0

someone know the issue ?

What does the server response payload look like?

1 Like

@dknutsen. what do u mean ? (i am new to that jersey thing). do u mean the “hello world” msg that i return ?

thats the msg out of the console: jquery.js:9013 Uncaught SyntaxError: Unexpected token P in JSON at position 0

  `at parse (<anonymous>)`

       ` at ajaxConvert (jquery.js:9013)`

    `    at done (jquery.js:9483)`

  `      at XMLHttpRequest.<anonymous> (jquery.js:9785)`

Yes, I mean like what is the exact payload that the server responds with? I ask because Ember Data expects the record data to be returned in response to a PUT/PATCH/POST (save). So your error is most likely caused by the server response being something other than what Ember Data wants. I was just guessing looking at your code (I’m unfamiliar with jersey) but if it’s just returning a 200 with payload “hello world” that’s definitely not going to work, and is most likely the cause of the message you’re seeing.

So if you’re using the RestSerializer, for example, Ember will expect the server’s response to be something like:

{
  'instantiatedwidget': {
    id: <server-generated-id>,
    userID: 1995,
    name: 'test',
    instanceID: 404
  }
}
1 Like

okay ty. that was a easy fix. i just couldnt find that ember expects these data like that.

Yeah it’s a very complex API all things considered and it’s easy to miss things. Glad you got it working!

1 Like