Ubiquity, jQuery & JSONP

0 views
Skip to first unread message

Yann Lossouarn

unread,
Jul 9, 2009, 11:12:21 AM7/9/09
to ubiquity...@googlegroups.com
Hello !

I'd like, from inside a Ubiquity command, to send JSON data to a server using a jQuery $.ajax (or whatever works) request, and then use the JSON data answered by the server following former request. I tried several flavors flavors, after having read tutorials and articles on the web. Here's current code I have :

      jQuery.ajax({
        type: "GET",
        url: "http://localhost/test/save_idx_json2.php",
        data: {json: strJSON},
        // dataType: "json",
        dataType: "jsonp",
        error: function() {
              CmdUtils.log("error - status not reported");
        },
        success: function(response) {
          CmdUtils.log("response : "+ response.data);
        }
      });



... and following code to handle the request server side :

<?php   
$json = json_encode(array("id"=>"John","nom"=>"Connor"));
if (isset($_GET['callback'])) {
    echo $_GET['callback'].'('.$json.');';
} else {
    echo $json;
}
?>

... which gives me this answer:

jsonp1247129384785({"id":"John","nom":"Connor"});


... but Firebug gives me this error :

missing ; before statement      hiddenframe.html (ligne 31)


... and this kind of log, server side :

127.0.0.1 - - [09/Jul/2009:16:59:18 +0200] "GET /test/save_idx_json2.php?callback=jsonp1247129384788&_=1247151558853&json=%257B%2522id%2522%253A%2522%2520H2751%2520%2522%252C%2522nom%2522%253A%2522gebus%2522%252C%2522race%2522%253A%2522humain%2522%252C%2522nombreDieux%2522%253A3%252C%2522dieu1%2522%253A%2522Gonzspidy%2522%252C%2522dieu2%2522%253A%2522Jiz%25C3%25BBskrys%2522%252C%2522dieu3%2522%253A%2522Feustla%2522%252C%2522niveau_min%2522%253A13%252C%2522niveau_max%2522%253A16%252C%2522pv_actuel_min%2522%253A100%252C%2522pv_actuel_max%2522%253A120%252C%2522pvmax_min%2522%253A399%252C%2522pvmax_max%2522%253A439%252C%2522regeneration_min%2522%253A5%252C%2522regeneration_max%2522%253A7%252C%2522attaque_min%2522%253A7%252C%2522attaque_max%2522%253A9%252C%2522defense_min%2522%253A7%252C%2522defense_max%2522%253A9%252C%2522degats_min%2522%253A7%252C%2522degats_max%2522%253A9%252C%2522PM_min%2522%253A8%252C%2522PM_max%2522%253A10%252C%2522DM_min%2522%253A4%252C%2522DM_max%2522%253A6%252C%2522vue_min%2522%253A5%252C%2522vue_max%2522%253A7%252C%2522armure_min%2522%253A5%252C%2522armure_max%2522%253A7%257D

I feel there may be something wrong with my approach (maybe this should be made only inside a page, does it make sense to use jsonp from ubiquity, as jsonp is used for cross-domain data exchange ?) , but can't put the finger on it...  On the other hand, I'm surprised to see that the Firebug error mentions "jsonp1234567" whereas the callback generated by jQuery is something like "jsonp1247129384785"... Any idea ?

Bye,
Yann



Yann Lossouarn

unread,
Jul 10, 2009, 12:46:25 PM7/10/09
to ubiquity-firefox
Hello,

I'm conscious my question is quite difficult to answer (at least quite
time demanding), but if you have an example of a good working jsonp
jQuery.ajax request running inside a ubiquity command, feel free to
send it to me, it may help...

Bye,
Yann



On 9 juil, 17:12, Yann Lossouarn <yann.lossou...@gmail.com> wrote:
> Hello !
>
> I'd like, from inside a Ubiquity command, to send JSON data to a server
> using a jQuery $.ajax (or whatever works) request, and then use the JSON
> data answered by the server following former request. I tried several
> flavors flavors, after having read tutorials and articles on the web. Here's
> current code I have :
>
> *      jQuery.ajax({*
>
> > *        type: "GET",*
> > *        url: "http://localhost/test/save_idx_json2.php",*
> > *        data: {json: strJSON},*
> > *        // dataType: "json",*
> > *        dataType: "jsonp",*
> > *        error: function() {*
> > *              CmdUtils.log("error - status not reported");*
> > *        },*
> > *        success: function(response) {*
> > *          CmdUtils.log("response : "+ response.data); *
> > *        }*
> > *      });*
>
> ... and following code to handle the request server side :
>
> *<?php    *
>
> > *$json = json_encode(array("id"=>"John","nom"=>"Connor")); *
> > *if (isset($_GET['callback'])) { *
> > *    echo $_GET['callback'].'('.$json.');'; *
> > *} else { *
> > *    echo $json; *
> > *} *
> > *?> *
>
> ... which gives me this answer:
> *
> *
>
>
>
> > *jsonp1247129384785({"id":"John","nom":"Connor"}); *
>
> ... but Firebug gives me this error :
>
> *missing ; before statement      hiddenframe.html (ligne 31)
>
> > ** echo "jsonp1234567(" . json_encode(a...("id"=>"John","nom"=>"Connor"))
> > . ");";\n
> > *
>
> ... and this kind of log, server side :
>
> *127.0.0.1 - - [09/Jul/2009:16:59:18 +0200] "GET
>
> > /test/save_idx_json2.php?callback=jsonp1247129384788&_=1247151558853&json=%257B%2522id%2522%253A%2522%2520H2751%2520%2522%252C%2522nom%2522%253A%2522gebus%2522%252C%2522race%2522%253A%2522humain%2522%252C%2522nombreDieux%2522%253A3%252C%2522dieu1%2522%253A%2522Gonzspidy%2522%252C%2522dieu2%2522%253A%2522Jiz%25C3%25BBskrys%2522%252C%2522dieu3%2522%253A%2522Feustla%2522%252C%2522niveau_min%2522%253A13%252C%2522niveau_max%2522%253A16%252C%2522pv_actuel_min%2522%253A100%252C%2522pv_actuel_max%2522%253A120%252C%2522pvmax_min%2522%253A399%252C%2522pvmax_max%2522%253A439%252C%2522regeneration_min%2522%253A5%252C%2522regeneration_max%2522%253A7%252C%2522attaque_min%2522%253A7%252C%2522attaque_max%2522%253A9%252C%2522defense_min%2522%253A7%252C%2522defense_max%2522%253A9%252C%2522degats_min%2522%253A7%252C%2522degats_max%2522%253A9%252C%2522PM_min%2522%253A8%252C%2522PM_max%2522%253A10%252C%2522DM_min%2522%253A4%252C%2522DM_max%2522%253A6%252C%2522vue_min%2522%253A5%252C%2522vue_max%2522%253A7%252C%2522armure_min%2522%253A5%252C%2522armure_max%2522%253A7%257D
> > *
>
> I feel there may be something wrong with my approach (maybe this should be
> made only inside a page, does it make sense to use jsonp from ubiquity, as
> jsonp is used for cross-domain data exchange ?) , but can't put the finger
> on it...  On the other hand, I'm surprised to see that the Firebug error
> mentions "***jsonp1234567" whereas *the callback generated by jQuery is
> something like "*jsonp1247129384785"*... Any idea ?
>
> Bye,
> Yann
> *
> *

brn.santanna

unread,
Jul 10, 2009, 2:42:31 PM7/10/09
to ubiquity-firefox
I have an example: http://tinyurl.com/mz5ewm


On Jul 9, 12:12 pm, Yann Lossouarn <yann.lossou...@gmail.com> wrote:
> Hello !
>
> I'd like, from inside a Ubiquity command, to send JSON data to a server
> using a jQuery $.ajax (or whatever works) request, and then use the JSON
> data answered by the server following former request. I tried several
> flavors flavors, after having read tutorials and articles on the web. Here's
> current code I have :
>
> *      jQuery.ajax({*
>
> > *        type: "GET",*
> > *        url: "http://localhost/test/save_idx_json2.php",*
> > *        data: {json: strJSON},*
> > *        // dataType: "json",*
> > *        dataType: "jsonp",*
> > *        error: function() {*
> > *              CmdUtils.log("error - status not reported");*
> > *        },*
> > *        success: function(response) {*
> > *          CmdUtils.log("response : "+ response.data); *
> > *        }*
> > *      });*
>
> ... and following code to handle the request server side :
>
> *<?php    *
>
> > *$json = json_encode(array("id"=>"John","nom"=>"Connor")); *
> > *if (isset($_GET['callback'])) { *
> > *    echo $_GET['callback'].'('.$json.');'; *
> > *} else { *
> > *    echo $json; *
> > *} *
> > *?> *
>
> ... which gives me this answer:
> *
> *
>
>
>
> > *jsonp1247129384785({"id":"John","nom":"Connor"}); *
>
> ... but Firebug gives me this error :
>
> *missing ; before statement      hiddenframe.html (ligne 31)
>
> > ** echo "jsonp1234567(" . json_encode(a...("id"=>"John","nom"=>"Connor"))
> > . ");";\n
> > *
>
> ... and this kind of log, server side :
>
> *127.0.0.1 - - [09/Jul/2009:16:59:18 +0200] "GET
>
> > /test/save_idx_json2.php?callback=jsonp1247129384788&_=1247151558853&json=%257B%2522id%2522%253A%2522%2520H2751%2520%2522%252C%2522nom%2522%253A%2522gebus%2522%252C%2522race%2522%253A%2522humain%2522%252C%2522nombreDieux%2522%253A3%252C%2522dieu1%2522%253A%2522Gonzspidy%2522%252C%2522dieu2%2522%253A%2522Jiz%25C3%25BBskrys%2522%252C%2522dieu3%2522%253A%2522Feustla%2522%252C%2522niveau_min%2522%253A13%252C%2522niveau_max%2522%253A16%252C%2522pv_actuel_min%2522%253A100%252C%2522pv_actuel_max%2522%253A120%252C%2522pvmax_min%2522%253A399%252C%2522pvmax_max%2522%253A439%252C%2522regeneration_min%2522%253A5%252C%2522regeneration_max%2522%253A7%252C%2522attaque_min%2522%253A7%252C%2522attaque_max%2522%253A9%252C%2522defense_min%2522%253A7%252C%2522defense_max%2522%253A9%252C%2522degats_min%2522%253A7%252C%2522degats_max%2522%253A9%252C%2522PM_min%2522%253A8%252C%2522PM_max%2522%253A10%252C%2522DM_min%2522%253A4%252C%2522DM_max%2522%253A6%252C%2522vue_min%2522%253A5%252C%2522vue_max%2522%253A7%252C%2522armure_min%2522%253A5%252C%2522armure_max%2522%253A7%257D
> > *
>
> I feel there may be something wrong with my approach (maybe this should be
> made only inside a page, does it make sense to use jsonp from ubiquity, as
> jsonp is used for cross-domain data exchange ?) , but can't put the finger
> on it...  On the other hand, I'm surprised to see that the Firebug error
> mentions "***jsonp1234567" whereas *the callback generated by jQuery is
> something like "*jsonp1247129384785"*... Any idea ?
>
> Bye,
> Yann
> *
> *
Reply all
Reply to author
Forward
0 new messages