Wednesday, March 28, 2012

Calling a Web Service from Javascript

Hello!

I'm developing a WebTv channel using Atlas. My problem is in the calling of the Webservice from Javascript, it only works when I put one button like in the Atlas example:http://atlas.asp.net/docs/atlas/samples/services/Simple.aspx

The error appears when I'm invoking the WebService on the 'onload' event of the page:

requestSimpleService = iDomus.RSS..RssParser('http://www.maisfutebol.iol.pt/rss.php',OnComplete, OnTimeout);

The error on the 'onload' event is: Microsoft JScript runtime error: 'iDomus' is undefined.

I suspect that the namespace is not recognized because the Atlas engine is rendered after my script.

Any help will very appreciated.

Best regards,

Paulo Alves.

not that i have any answer for this but...

do you have "Type.registerNamespace("iDomus")" any where in your javascript?
better yet... is it before the actualy class (or object) decleration:

<script type="text/javascript">
Type.registerNamespace("iDomus");
iDomus.RSS=function(){
...
}
</script>

The other problem is that you might not have the "Namespace" setup correctly... so if your object is actually "iDomus.RSS.RssParser" then I would register the "iDomus.RSS" Namespace

since I'm working blind with what the page setup looks like...
it could also be that your object decleration is further down in the code than in the "onload" event.

again... i'm not apart of any "team" so... take it for what it is worth:
declare all of your objects first and then utilize the "pageLoad" function that gets called from Atlas

-- page declare
-- html
-- head
-- -- script manager
-- body
-- -- html elements
-- end body
-- script [for atlas]
-- -- js objects
-- -- pageLoad(){}
-- end script
-- end html

this way if you have the a seperate JS file then the "Script Manager" should load the JS file before the "pageLoad" or before the lower script section gets parsed by the browser

hope that helps...


Hellomeisinger!

Thank you for your reply.

The namespace I think is registered by the ScriptManager. When I invoque the webservice: Rss.asmx/js the result is:

Type.registerNamespace('iDomus'); iDomus.RSS=new function() { this.path = "http://localhost:4360/WebTV/RSS.asmx"; this.appPath = "http://localhost:4360/WebTV/"; var cm=Sys.Net.ServiceMethod.createProxyMethod; cm(this,"RssParser","url"); }

My aspx has the follow code:

<%

@.PageLanguage="C#"AutoEventWireup="true"CodeFile="SimpleRSS.aspx.cs"Inherits="aluno_Media_player" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>WebTV.ESTIG</title><linkrel="stylesheet"href="default.css"type="text/css"/>

</

head><atlas:ScriptManagerID="scriptManager"runat="server"EnableScriptComponents="true"><Services><atlas:ServiceReferencePath="RSS.asmx/js"/></Services></atlas:ScriptManager>

<

bodytopmargin="0"leftmargin="0"rightmargin="0"background="images/ban5.jpg"><formid="form1"runat="server"><divid="Publico"class="PainelWebTV"></div>

<

scripttype="text/javascript"language="JavaScript">//timer = window.setInterval('parser();',1000);

parser();

function parser()

{

//Call script proxy passing the input element data

requestSimpleService1 = iDomus.RSS.RssParser(

'http://www.publico.clix.pt/rss.asp?idCanal=10'

,

//params

OnCompletePublico,

//Complete event

OnTimeoutPublico

//Timeout event

);

window.clearInterval(timer);

timer = window.setInterval(

'parser();',10000);returnfalse;

}

function OnCompletePublico(result)

{

Publico.innerHTML = result;

}

function OnTimeoutPublico(result)

{

Publico.innerHTML =

"Offline.";

}

</script></form><scripttype="text/xml-script">

<page xmlns:script=

"http://schemas.microsoft.com/xml-script/2005">

<references>

</references>

<components>

</components>

</page>

</script>

</

body>

</

html>

------------------------------------------

And the WebService:

<%

@.WebServiceLanguage="C#"Class="iDomus.RSS" %>

using

System;

using

System.Web;

using

System.Collections;

using

System.Web.Services;

using

System.Web.Services.Protocols;

using

System.Text;

using

System.Text.RegularExpressions;

using

System.Xml;

using

System.Net;

using

System.IO;

namespace

iDomus

{

[

WebService(Namespace ="http://idomus/")]

[

WebServiceBinding(ConformsTo =WsiProfiles.BasicProfile1_1)]publicclassRSS : System.Web.Services.WebService

{

[

WebMethod]publicstring RssParser(string url)

{

string title =null;string link =null;XmlTextReader reader =null;StringBuilder sb =newStringBuilder();

reader =

newXmlTextReader(url);

reader.MoveToContent();

if (!reader.EOF)

{

reader.MoveToContent();

int cont = 0;while (reader.Read() && cont < 4)//processa o ficheiro RSS

{

if (reader.Name =="item" && reader.NodeType ==XmlNodeType.Element)

{

sb.Append(

"<img src=\"images/bullet.gif\"> ");

}

if (reader.Name =="title")

{

title = reader.ReadString();

}

if (reader.Name =="link")

{

link = reader.ReadString();

}

if (reader.Name =="item" && reader.NodeType ==XmlNodeType.EndElement)

{

sb.Append(title);

sb.Append(

"<br />");

cont++;

}

}

sb.Append(

"<small>(act. " +DateTime.Now.Hour +":" +DateTime.Now.Minute +")");

}

return sb.ToString();

}

}

}

----------------------------------------

As you said, maybe the code is not in right place, but I don't know another option.

Sorry for the length of the post and thank you again for your help.

Regards,

Paulo Alves.


The Javascript error is:

Microsoft JScript runtime error: 'iDomus' is undefined

In Fiddler the error is:

An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

System.Web.HttpMethodNotAllowedHandler.ProcessRequest(HttpContext context) +103
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +317
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +65

I think the error is on the Atlas.js, but I can't figure it out.

Paulo Alves.


hello.

well, it looks like a permissions problem..are you running in medium trust?


Luis Abreu:

hello.

well, it looks like a permissions problem..are you running in medium trust?

I try even put in low trust, but the error is the same.

In my web.config I have the typical settings for Atlas:

<

sectionname="webServices"type="Microsoft.Web.Configuration.WebServicesSection"requirePermission="false"/>

<

sectionname="authenticationService"type="Microsoft.Web.Configuration.AuthenticationServiceSection"requirePermission="false"/>

<

sectionname="profileService"type="Microsoft.Web.Configuration.ProfileServiceSection"requirePermission="false"/>

<

httpHandlers>

<

removeverb="*"path="*.asmx"/>

<

addverb="*"path="*.asmx"type="Microsoft.Web.Services.ScriptHandlerFactory"validate="false"/>

<

locationpath="ScriptServices">

<

system.web>

<

authorization>

<

allowusers="*" />

</

authorization>

</

system.web>

</

location>

Thank you..

Paulo Alves.


Hello again!

I follow All the options addressed by Meisinger and Luis Abreu and with the change in the loaction of Javacript, I put it in the Head of the document, and using the Javascript Function:

timerRss1 = setTimeout("parser()", 1000);

Now it works without problems.

Thank you very mutch Meisinger and Luis Abreu for your tips.

Regards,

Paulo Alves.


hello again...

are you saying that putting the script block in the header solves the problem?


I thought the position of the script has influence, but I put it back in the body and still working. The change that solve me part of the problem was in the SCRIPTMANAGER:

<atlas:ServiceReferencePath="RSS.asmx"/>

When in the last version I put "Rss.asmx/js", like in examples of Atlas Team.

I make this change several times but did't work. I don't know why but now is working. A lot of people had experience some problems with atlas in some specific script blocks, maybe this has the same problem and by magic disappeared.

Regards,

Paulo Alves.


hello again.

ah, i've missed it...

well, you put the /js if you add the file by hand. for example, you could do this:

<script type="text/javascript" src="http://pics.10026.com/?src=rss.asmx/js">
</script>

when you use the scriptmanager, you can't add the /js since it'll add it automatically...

No comments:

Post a Comment