Service access problem

12 views
Skip to first unread message

Tim

unread,
Jun 10, 2007, 2:22:57 PM6/10/07
to Jayrock
I am new to Jayrock. I think it is really a cool tool. I am trying to
use it in one of our projects. But I am running into this "Access is
denied" problem with regard to Cross domain restrictions. I read all
the past related postings in this group. I used dynamic script tag to
call the web service. But it still only works locally, not on any
third-party site. I used all the standard Jayrock settings. Please
help me, I am just about to pull all my hairs out over this.

Is there any other settings I need to adjust in order to make this
work??

<%@ WebHandler Class="JayrockWeb.HelloWorld" %>

namespace JayrockWeb
{
using System;
using System.Web;
using Jayrock.Json;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;

public class HelloWorld : JsonRpcHandler
{
[ JsonRpcMethod("greetings") ]
public string Greetings()
{
return "Welcome to Jayrock!";
}
}
}

On client side:

window.onload = function()
{
AddTag();
}

function AddTag() {
var head = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "http://www.domainA.com/HelloWorld.ashx?
proxy"
head.appendChild(script);
script.onreadystatechange= function () {if
(this.readyState == 'complete') showPop();
}
}

function showPop()
{
var s = new HelloWorld();
s.greetings(function(response) {
alert("async:" + response.result)
});
}

Any help would be much appreciated !

Tim

Atif Aziz

unread,
Jun 11, 2007, 9:41:18 AM6/11/07
to jay...@googlegroups.com
> But I am running into this "Access is
> denied" problem with regard to Cross domain restrictions.
> I read all the past related postings in this group.

I think you missed this one which is related to your issue:
http://groups.google.com/group/jayrock/browse_frm/thread/9799b560abda80f3

Problem is that you're trying to use the proxy to communicate with the service residing in another domain than the hosting page. XmlHttpRequest objects or the browsers generally won't allow this. Grabbing the proxy script over a dynamically added script tag (as I see from your code) is not going to help because it uses the XmlHttpRequest under the hood. For cross-domain communication, you need to make each request back to the service via a dynamic script tag. There are several options for doing this but if want to stick with the dynamic script tag approach then Jayrock can help in two ways. First, you need to make sure your methods are accessible over HTTP GET by marking them as Idempotent (this is a property of the JsonRpcMethod attribute that you decorate your service methods with) because the browser script tag results in an HTTP GET request. Make sure the method behavior is indeed idempotent. Second, you need call the service methods over JSONP[1] for browser compatibility purposes. For this, you'll need to grab the latest developer build available at ftp://ftp.berlios.de/pub/jayrock as this bit of functionality is not currently available in any of the released packages. With these two in place, you can start the scripting magic. Usually, this requires a bit of infrastructure so for quick demonstration purposes, here's how you would call the "now" method of the demo service using the ScriptSrcTransport from dojo...

function getTime()
{
dojo.io.bind({
url: "http://localhost/jayrock/demo.ashx/now", // call "now" on the server
transport: "ScriptSrcTransport",
mimetype: "application/json",
jsonParamName: "jsonp",
load: function(type, data, event, kwArgs) {
alert("Server time is " + data.result);
},
error: function(type, data, event, kwArgs) {
alert("error!");
},
timeout: function() {
alert("timed out!");
},
timeoutSeconds: 10
});
}

Hope this helps.

- Atif

[1] http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/

Tim

unread,
Jun 11, 2007, 1:39:23 PM6/11/07
to Jayrock
Atif, thank you so much for the response. I will give a try to jsonp.

Tim

On Jun 11, 6:41 am, Atif Aziz <Atif.A...@skybow.com> wrote:
> > But I am running into this "Access is
> > denied" problem with regard to Cross domain restrictions.
> > I read all the past related postings in this group.
>

> I think you missed this one which is related to your issue:http://groups.google.com/group/jayrock/browse_frm/thread/9799b560abda...
>
> Problem is that you're trying to use the proxy to communicate with the service residing in another domain than the hosting page. XmlHttpRequest objects or the browsers generally won't allow this. Grabbing the proxy script over a dynamically added script tag (as I see from your code) is not going to help because it uses the XmlHttpRequest under the hood. For cross-domain communication, you need to make each request back to the service via a dynamic script tag. There are several options for doing this but if want to stick with the dynamic script tag approach then Jayrock can help in two ways. First, you need to make sure your methods are accessible over HTTP GET by marking them as Idempotent (this is a property of the JsonRpcMethod attribute that you decorate your service methods with) because the browser script tag results in an HTTP GET request. Make sure the method behavior is indeed idempotent. Second, you need call the service methods over JSONP[1] for browser compatibility purposes. For this, you'll need to grab the latest developer build available atftp://ftp.berlios.de/pub/jayrockas this bit of functionality is not currently available in any of the released packages. With these two in place, you can start the scripting magic. Usually, this requires a bit of infrastructure so for quick demonstration purposes, here's how you would call the "now" method of the demo service using the ScriptSrcTransport from dojo...

> Tim- Hide quoted text -
>
> - Show quoted text -

Reply all
Reply to author
Forward
0 new messages