Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get data with ajax request #20

Open
decapo01 opened this issue Mar 17, 2017 · 3 comments
Open

How to get data with ajax request #20

decapo01 opened this issue Mar 17, 2017 · 3 comments

Comments

@decapo01
Copy link

I'm having trouble getting data with an ajax request.

I have the code

val myPromise = $.ajax(url)

myPromise.done((data: Any, textStatus: String, jqXHr: JQueryXHR) => {
  println(data)
})

in the console log I get [object Object]

if I try to match data I get

Uncaught TypeError: Cannot read property 'getName__T' of null

I checked the textStatus and it returns success. I've also checked the request in the chrome dev tools and it is returning a json string. How do I use that json string after I've recieved it?

@jducoeur
Copy link
Owner

I don't have much experience here -- I do all of my Ajax work using the JS wrappers provided by Play, rather than making raw Ajax calls through jQuery.

That said, the error you're getting on matching suggests that something is wrong with the match. Are you matching a facade? Are you sure that all of the fields are non-null? This sort of error is extremely common when you are trying to cast a JS object to a facade, and one or more fields are null but the facade doesn't take that into account.

@decapo01
Copy link
Author

I've found that if you change the Type from Any to js.Dynamic you can use the data.

e.g. if the data object had properties id and name you could use the data like so

myPromise.done((data: js.Dynamic, textStatus: String, jqXHr: JQueryXHR) => {
  val id = data.id
  val name = data.name
  println(s"the id is $id and the name is $name")
})

You might be right about the match though. The data comming back from the server is missing fields for the None values that are set. However, this is the desired behavior for now so I will use it as I have above.

@jducoeur
Copy link
Owner

Note that, if a field of type T might be empty, it shouldn't be marked in the facade as : T, but as : UndefOr[T]. That tells the compiler to be prepared for it to be missing, and is often the fix for this sort of error...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants