configuring jsonp in a service

11 views
Skip to first unread message

Mike Pearson

unread,
Oct 17, 2011, 6:02:11 PM10/17/11
to mobl

Zef,

I'm using a site that has a JSONP API, but rather than using &callback=? (the default jQuery jsonp parameter) it uses &JSONP=?.

jQuery supports this - you add in jsonp="JSONP" in the parameters to the $.ajax call, but there appears to be no way to insert this parameter in the mobl service definition. Result is that I'm editing the generated javascript.

I guess I could do a messy override of $.ajax to avoid this, but I'm wondering if there's a better way?

--

Mike Pearson

Millennium Mathematics Project, 

Centre for Mathematical Sciences,

University of Cambridge

Cambridge CB3 0WA

Zef Hemel

unread,
Oct 18, 2011, 2:29:15 AM10/18/11
to mo...@googlegroups.com
Yikes, editing the generated code is definitely a bad idea :-)
Probably the simplest way to fix it is to write a Javascript function
for it:

<javascript>
__ns.fetchSomething = function(..., callback) {
$.ajax({
url: "...",
jsonp: "JSONP",
success: callback
});
};
</javascript>

external function fetchSomething(...) // note: no callback

and then you can just call fetchSomething with your arguments

HTH,

Zef

--
Zef Hemel
http://zef.me
http://twitter.com/zef

Mike Pearson

unread,
Oct 18, 2011, 5:00:33 AM10/18/11
to mo...@googlegroups.com
Zef, I think I'm missing something.

Are saying that I should simply bypass the mobl Service and resource definition? Aren't they doing other magic like hooking up the mappers?

The mystery site is Tesco BTW. Code below. I don't understand how you would hook up the resource definition with a custom 'fetchSomething()'?

Mike

service Tesco {

  resource login(email:String, password:String, developerKey:String, apiKey:String) : TescoLogin {

    uri = "http://www.techfortesco.com/groceryapi_b1/restservice.aspx?command=LOGIN&email=" + email + "&password=" + password + "&developerkey=" + developerKey + "&applicationkey=" + apiKey

    encoding = "jsonp"

    mapper = TescoLoginMapper

  }

  

Zef Hemel

unread,
Oct 18, 2011, 5:09:17 AM10/18/11
to mo...@googlegroups.com
Ah, you're using services. Yes, well, there's surprisingly little
magic there ;) The mapper function is just a function called on the
JSON result, i.e. this particular service resource thing is equivalent
to:

var result = TescoLoginMapper(httpRequest("http://www.techfortesco.com/groceryapi_b1/restservice.aspx?command=LOGIN&email="


+ email + "&password=" + password + "&developerkey=" + developerKey +

"&applicationkey=" + apiKey, encoding="jsonp"));

thus, service definitions do not add all that much over a simple
httpRequest (http://docs.mobl-lang.org/mobl). So to replace it with a
native function call also doesn't matter that much.

Zef

Mike Pearson

unread,
Oct 18, 2011, 5:47:43 AM10/18/11
to mo...@googlegroups.com
OK, thanks. Got it.

Mike

kuyakoy

unread,
Nov 10, 2011, 1:42:05 PM11/10/11
to mobl
Zef,

Using jquery's ajax, I can get my result from the callback function.
But how do I bring the data I got back in to my screen? All I was able
to do is alert the response.

Thanks!
Michael

Chris Melman

unread,
Nov 10, 2011, 3:35:00 PM11/10/11
to mo...@googlegroups.com
there are mulitple ways to do this, one could be to use
whenLoaded(variable){
        page content
}
and let your callbackfunction set this variable
 
 
 
> Date: Thu, 10 Nov 2011 10:42:05 -0800
> Subject: [mobl] Re: configuring jsonp in a service
> From: mjpo...@gmail.com
> To: mo...@googlegroups.com

Zef Hemel

unread,
Nov 11, 2011, 11:40:23 AM11/11/11
to mo...@googlegroups.com
What do you mean using the jQuery syntax?

Zef

--

MIchael Pongan

unread,
Nov 11, 2011, 12:36:12 PM11/11/11
to mo...@googlegroups.com
this syntax
<javascript>
> __ns.fetchSomething = function(..., callback) {
>   $.ajax({
>     url: "...",
...
>   });};
>
> </javascript>

Totemteleko

unread,
Nov 14, 2011, 9:39:56 AM11/14/11
to mobl
Zef, could you share and example of server what makes a jsonp
response?? I'm not able to create it, I just want to send a .json,
already created in the server, I don't need exchange params. Thanks
Reply all
Reply to author
Forward
0 new messages