Get JSON from different domain

224 views
Skip to first unread message

Geoffrey Lalloue

unread,
May 20, 2013, 11:49:05 AM5/20/13
to javam...@googlegroups.com
Hi,

I try to get JSON from my Java Melody centralized server by javascript request.
Because of same origin policy, i cannot get it (different domain between centralized server and webpage which call java melody api).
So i tried to use JSONP, but Java Melody does not seem to manage it.

It would be really nice to add support for jsonp, which is not very complicated to implement in server side (at least I hope).

Just for information :

JSON looks like this:
{ "name": "value" }
and it's called as 
<script type="text/javascript" src="http://example.com/json"></script>

Whereas JSONP looks like this:
functionName({ "name": "value" });
and it's called as 
<script type="text/javascript" src="http://example.com/json?callback=functionName"></script>

Vernat Emeric

unread,
Jun 4, 2013, 5:36:20 PM6/4/13
to javam...@googlegroups.com
Hi Geoffrey,

I have written what could be a possible patch. It is attached in this mail as "jsonp.patch".
I have been able to use the patch with the following html:
<script type = "text/javascript">
    window.melodyCallback = function(data) {
      console.log(JSON.stringify(data));
    };
</script>
<script type="text/javascript" src="http://localhost:8080/mywebapp/monitoring?format=jsonp"></script>
Note the jsonp format and the "melodyCallback" function.

If you want to try it, I have made a local build and you can download it at:
http://javamelody.googlecode.com/files/javamelody-20130604.jar
What do you think?

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

jsonp.patch

Geoffrey Lalloue

unread,
Jun 6, 2013, 4:16:55 AM6/6/13
to javam...@googlegroups.com
Hi Emeric,

Thank you very much for this patch.
I'll try it as soon as possible in my environment and I'll say to you if everything works perfectly for me.

bye,
Geoffrey

Geoffrey Lalloue

unread,
Jun 7, 2013, 11:34:20 AM6/7/13
to javam...@googlegroups.com
For the moment, it's working good.
I get my JSON by this javascript code :

$.ajax({
        url : srvUrl,
        dataType : 'jsonp',
        jsonp : 'melodyCallback',
        success : function(data){
            console.log(JSON.stringify(data));
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log(xhr.status + " / " + thrownError);
        }       
    });

And my callback method (melodyCallback) is called perfectly.

I will continue my tests next week but would it be possible to integrate the management of "jsonp" to ".war" centralized server please?

Have a good weekend
Reply all
Reply to author
Forward
0 new messages