0

When i post from angularjs

{name:"John", age: 26} 

i get BadRequest, however if is manually post

{"name":"John", "age": 26} 

it works

in the Scala/Play side its the simple case class with Json formatting

import play.api.libs.json._
import play.api.libs.functional.syntax._

case class Customer(name: String, age: Int)

implicit val customerFormat = Json.format[Customer]

the Action is a simple one

def save = Action(parse.json) { request =>
  request.body.validate[Customer].map { customer =>
      myDAO.saveCustomer(customer)
      Ok(toJson(customer))
    }.getOrElse(BadRequest("invalid json"))
  })
}

i guess the answer is either make angularjs quote the keys, or make play to ignore the the lack of keys, i'll need help on how do i do either of it, or am i missing something

1
  • How do you build and send the JSON-like data from AngularJS? Do you build it manually or use a library? Commented Sep 24, 2014 at 21:14

1 Answer 1

2

In valid JSON, object keys must always be quoted. Try typing the object literal without the quotes into a JSON validator for confirmation.

It's important to note that there are differences between plain old Javascript object literals (POJOs) and JSON, with the JSON format being stricter. JSON is a string data type that happens to be valid Javascript. Technically, you obtain JSON data from Javascript code by stringifying a POJO:

JSON.stringify({name:"John", age: 26})
// "{"name":"John","age":26}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.