ajax query problem

261 views
Skip to first unread message

Henry Story

unread,
Dec 24, 2013, 7:43:53 PM12/24/13
to scal...@googlegroups.com
Hi I have been trying a little ajax query with the following code

    jQ.ajax(js.Dictionary(
      "url" -> "http://www.w3.org/People/Berners-Lee/card#i".asInstanceOf[js.Any],
      "success" ->  { (data: js.Any, textStatus: js.String, jqXHR: JQueryXHR) =>
        console.log(s"data=$data,text=$textStatus,jqXHR=$jqXHR");
        js.Dictionary().asInstanceOf[js.Dynamic]
      }.asInstanceOf[js.Any],
      "error" ->  { ( jqXHR: JQueryXHR, textStatus: js.String, errorThrow: js.String) =>
        console.log(s"jqXHR=$jqXHR,text=$textStatus,err=$errorThrow");
        js.Dictionary().asInstanceOf[js.Dynamic]
      }.asInstanceOf[js.Any],
      "type" -> "GET".asInstanceOf[js.Any]
    ).asInstanceOf[JQueryAjaxSettings]
    )

This fetches the url, but it does not call either the error or the success functions.
I used the 0.1 and the 0.2-SNAPSHOT compiler for this.

Any ideas?

Henry

Sébastien Doeraene

unread,
Dec 25, 2013, 4:29:46 AM12/25/13
to Henry Story, scal...@googlegroups.com

Hi,

You cannot use .asInstanceOf[js.Any] like this and expect it to work. You have to use the appropriate implicit conversions, which are not no-ops for all types. So use ascription x: js.Any instead.

Cheers,
Sébastien

--
You received this message because you are subscribed to the Google Groups "Scala.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-js+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Henry Story

unread,
Dec 25, 2013, 5:18:26 AM12/25/13
to scal...@googlegroups.com, Henry Story
Thanks Sébastien, this worked:

    jQ.ajax(js.Dictionary(
      "url" -> {val str: js.Any = "http://www.w3.org/People/Berners-Lee/card"; str},
      "success" ->  { val res: js.Any =  test _; res },
      "error" -> { val res: js.Any = err _; res } ,
      "type" -> { val str: js.Any = "GET"; str }
    ).asInstanceOf[JQueryAjaxSettings]
    )

Not very beautiful of course. So I tried the JsObjectBuilder by Benjamin Jackman

success = (data: js.Any, textStatus: js.String, jqXHR: JQueryXHR) =>{
console.log(s"data=$data,text=$textStatus,jqXHR=$jqXHR");
js.Dictionary()
},
error = ( jqXHR: JQueryXHR, textStatus: js.String, errorThrow: js.String) => {
console.log(s"jqXHR=$jqXHR,text=$textStatus,err=$errorThrow");
js.Dictionary()
},
`type` = "GET"
)

which looks a lot better and compiles.

But it gives me a 

Uncaught TypeError: Object #<Object> has no method 'scala_Nothing'


( It does go into the applyDynamicNamed part and write out 
  applyDynamicNamed(apply)(args: WrappedArray((url,http://www.w3.org/People/Berners-Lee/card), (success,function () { [native code] }), (error,function () { [native code] }), (type,GET))
)

Henry Story

unread,
Dec 25, 2013, 7:22:16 AM12/25/13
to scal...@googlegroups.com, Henry Story
I found the answer!

I had forgotten to type the result of jsObj! This works:

   val json = jsObj[org.scalajs.jquery.JQueryAjaxSettings](url = "http://www.w3.org/People/Berners-Lee/card",
            success =   (data: js.Any, textStatus: js.String, jqXHR: JQueryXHR) =>{
              console.log(s"data=$data,text=$textStatus,jqXHR=$jqXHR");
              js.Dictionary()
            },
            error =  ( jqXHR: JQueryXHR, textStatus: js.String, errorThrow: js.String) => {
              console.log(s"jqXHR=$jqXHR,text=$textStatus,err=$errorThrow");
              js.Dictionary()
            },
          `type` = "GET"
    )
    jQ.ajax(json)

All the best! :-)

Sébastien Doeraene

unread,
Jan 5, 2014, 6:17:32 AM1/5/14
to Henry Story, scal...@googlegroups.com
Hi,

With 0.2-SNAPSHOT, you should now be able to write:

jQ.ajax(js.Dynamic.literal(

    url = "http://www.w3.org/People/Berners-Lee/card",
    success = { (data: js.Any, textStatus: js.String, jqXHR: JQueryXHR) =>
      console.log(s"data=$data,text=$textStatus,jqXHR=$jqXHR")
    },
    error = { (jqXHR: JQueryXHR, textStatus: js.String, errorThrow: js.String) =>
      console.log(s"jqXHR=$jqXHR,text=$textStatus,err=$errorThrow")
    },
    `type` = "GET"
).asInstanceOf[org.scalajs.jquery.JQueryAjaxSettings])

Cheers,
Sébastien
Reply all
Reply to author
Forward
0 new messages