Showing posts with label webservice. Show all posts
Showing posts with label webservice. Show all posts

Wednesday, March 28, 2012

Why my web services cant accept parameter?

When my javascript call the echoString(s) in the following code, it return nothing

<%@dotnet.itags.org. WebService Language="C#" Class="WebService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {

[WebMethod]
public string echoString(string s) {
return s;
}

}

The strange things are my .NET program works fine with the code above and my javascript client program doesn't work with the code above but works fine with other people's web services. EX:

http://www.mssoapinterop.org/asmx/simple.asmx?op=echoString

What is wrong with my code? Thank you!!!!!!!!!

asdpai:

what is wrong with my code?

nothing, code is correct. Decorate youre WebService with theSystem.Web.Script.Services.ScriptService() and the WebMethode with the ScriptMethod() attribute eg.

<%@. WebService Language="C#" Class="WebService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
usingSystem.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class WebService : System.Web.Services.WebService {

[ScriptMethode()]
[WebMethod]
public string echoString(string s) {
return s;
}

}

Hope that coul help.

why script enabled webservice making postback to server?

My WebPage has two textbox & one button.On button click I call script enabled webservice asynchronously still why my page go for postback to server?

<%@dotnet.itags.org.PageLanguage="C#"AutoEventWireup="true"CodeFile="page2.aspx.cs"Inherits="page2" %>

<!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>Untitled Page</title>

<scriptlanguage="javascript"type="text/javascript">

<!--

function Button1_onclick()

{

ret= WebService.Hello(document.getElementById('TextBox1').value,OnComplete,OnTimeOut,OnError);

return(true);

}

function OnComplete(args)

{

document.getElementById('TextBox2').value=args;

}

function OnTimeOut(args)

{

alert('Timeout');

}

function OnError(args)

{

alert('error');

}

// -->

</script>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>

<asp:ButtonID="Button1"runat="server"Text="Button"OnClientClick="Button1_onclick();"/>

<asp:TextBoxID="TextBox2"runat="server"></asp:TextBox>

<asp:ScriptManagerID="ScriptManager1"runat="server">

<Services>

<asp:ServiceReferencePath="WebService.asmx"/>

</Services>

</asp:ScriptManager>

</div>

</form>

</body>

</html>

===============================================

WebService

using System;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

///<summary>

/// Summary description for WebService

///</summary>

[WebService(Namespace ="http://tempuri.org/")]

[WebServiceBinding(ConformsTo =WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService()]

publicclassWebService : System.Web.Services.WebService {

public WebService () {

//Uncomment the following line if using designed components

//InitializeComponent();

}

[WebMethod]

publicstring Hello(string msg) {

return"Hello"+msg;

}

}

function Button1_onclick()

{

ret= WebService.Hello(document.getElementById('TextBox1').value,OnComplete,OnTimeOut,OnError);

return false; // return(true); you need to return false instead of true. if you return false it doesn't do post back.

}

Thanks,

Kiran


Still same condition it does post back.I tried same application with HTML controls textboxes & button it works fine but when I tried with server side control it posts back


Hi,

can you post code that you used for testing.

Thanks,

Kiran


here is code with HTML textboxes & button for same script enabled webservice

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

<scriptlanguage="javascript"type="text/javascript">

<!--

function Button1_onclick()

{

ret= WebService.Hello(document.getElementById('Text1').value,OnComplete,OnTimeOut,OnError);return(true);

}

function OnComplete(args)

{

document.getElementById('Text2').innerText=args;

}

function OnTimeOut(args)

{

alert('Timeout');

}

function OnError(args)

{

alert('error');

}

// -->

</script> </head>

<body>

<formid="form1"runat="server">

<asp:ScriptManagerID="ScriptManager1"runat="server">

<Services>

<asp:ServiceReferencePath="WebService.asmx"/>

</Services>

</asp:ScriptManager>

<div>

<inputid="Text2"type="text"/>

<inputid="Text1"type="text"/>

<inputid="Button1"type="button"value="button"language="javascript"onclick="return Button1_onclick()"/>

</div>

</form> </body>

</html>


Hi,

function Button1_onclick() still returns true.

did you change it to return false. and tried it.

Thanks,

Kiran


yeah.......I did change it to false & tested it ,it does post back .The code which I posted doesn't postback but its withHTML control which works fine & I am trying to do same webservice code but with server control.

Hi,

can you change below code

<asp:ButtonID="Button1"runat="server"Text="Button"OnClientClick="Button1_onclick();"/>

to

<asp:ButtonID="Button1"runat="server"Text="Button"OnClientClick="return Button1_onclick();"/>

make sure that function is called. keep alert message in Button1_onclick function so you know that function got invoked when you click Button1_onclick.

Thanks,

Kiran


Thank you Kiran it works just fine.But I didn't get difference b/nButton1_onclick() & return Button1_onclick() could you please explain it?

Thanks

Manisha.


event = "return function()" if function returns false the event gets canceled.

if your case we are forcefully canceling onclick event by returning false.

event = "function()" doesn't matter what function returns.

Thanks,

Kiran

Why webservices?

I don't know if I haven't understood the right way to use Atlas, but as far as I know all the calls are done to a webservice? How is this helpful? Why does it have to be this way? Why not call methods defined in the asp page?

Thanks for the help

Atlas lets you call both methods on Web Services and methods defined on the page. Using a Web Service is cleaner if you want to make some general functionality available to multiple pages, but there are also scenarios where calling methods on the page is convenient.

David


Well, I guess whatever you need to do the job is what you'd choose. Atlas streams XML based information to and from the server - thereby making webservices a perfect fit. By your statement "calls methods in the page", I suppose you are referring to server side calls... In this case, postbacks are no longer required - Web 2.0 is coming, where a richer UI experience can be achieved by asynchronously updating the bits of data on a screen that are needed - not the entire page for the sake of postbacks. IMHO, Atlas is a great step forward for SOA and SaaS...

Jon


Hello,
I am not getting how can i make the script part dynamic...
In detail...i want to show a Chart image when user hovers over a stocksymbol(ex.TCS or MSFT) but there can be many such symbols on the page,(another solution is that i add <script></script> for eachsymbol at client side...but its obviously not feasible) so how can imake the <script></script> part of our Client dyanamic...

hope u can help
Devang

Monday, March 26, 2012

With all the webservices, what is the right way to handle security?

We need tell webservice what to return, if a webservice is a general one, we need pass paramters to it to tell what data it should return. I don't think it is a good idea to expose the parameters to clients. So I am think I will put some parameters for a webserice to session state. then give a GUID to webservice, with the GUID, the webservice can get the parameters from session state. But I found my webservice can not read the variable from session state. (they are on same site).

This really depends on the specific web service. If you are dealing with something that returns very public data (e.g. a weather service), then security is not much of a concern. But if your web service has the potential to fo bad things on the client, or return confidential data, you clearly have to be careful. Authentication is one way to deal with this.

David


Sorry, I forgot to address your other question. In order to use session, you need to use this attribute on the method:

[WebMethod(EnableSession = true)]


thanks.

I just need some guide from you, so I don't spend time on wrong direction.

As the EnableSession, I posted in another post. It doesn't work. The reason is I need put that on GetData, but GetData can not be override.

Please give me some idea, I really need it.

Saturday, March 24, 2012

Would this be practical?

I was thinking it might be nice to have a search user screen which in the background uses a webservice to call out and retrieve a list of users from the db and use the autocomplete method I have seen used before - I'm not worried about security its just would this by using atlas to do this would this be a reasonable thing to do?

hello.

well, you could do that if you have a web service that returns that info...

Wednesday, March 21, 2012

Wrong URL for Webservice

Hello

I've a Problem with Webservice Calls and/or Scripttags (not sure)
In the default.aspx I've the following Script declaration:

<asp:ScriptManager ID="scriptManager" runat="server" ScriptMode="debug">
<services>
<asp:servicereference path="~/webservices/myWebservice.asmx" />
<services>
<asp:ScriptManager /
Some times the Service is called with this URL: http://www.mySite.com/webservices/myWebservice.asmx/webservices.myWebservice
The extra "/webservices.myWebservice" is wrong and the Page could not be found.

I'm sure the wrong URL is generated from Javascript (in one of the JS-Files from the Framework) when executing the functions in myWebservice or from the generated HTML-Code from the Default.aspx Page (something like <script language="javascript" src="http://pics.10026.com/?src=http://www.mySite.com/webservices/myWebservice.asmx/webservices.myWebservice />)

Any Ideas how to solve this error? Are there any Updates for the AJAX-Framework?

Your webservice is only like thishttp://www.mySite.com/webservices/myWebservice.asmx

there is no need for webservices.myWebservice /

try it


This is the normal way a webservice method is called. There is nothing wrong in it.

The method name is appended to the webservice url

For example, if you are calling a method called method1 on webservice.asmx, the url will look like

http://.../webservice.asmx/method1


But why do I get errors like the following:

Ausnahmeinformationen:
Ausnahmetyp: InvalidOperationException
Ausnahmemeldung:Request format is unrecognized for URL unexpectedly ending in '/webservices.myWebservice'.

Anforderungsinformationen:
Anforderungs-URL:http://www.mySite.ch/webservices/myWebservice.asmx/webservices.myWebservice
Anforderungspfad: /webservices/myWebservice.asmx/webservices.myWebservice

Threadinformationen:
Thread-ID: 1
Stapelüberwachung: bei System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response)
bei System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath)
bei System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
bei System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
bei System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
bei System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

If I try to call the webservice directly, I get the same error in Browser.


Check if you have this in the web.config. I have seen this to cause this error

<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>


Thank you for the tipp. That code is not in the web.conf. I'll try if it helps.


I would guess that your web.config is missing these entries:

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpHandlers>

XML and Webserivces

Hi All,

I have created a webservice that returns XML data to be processed by the client.

The returned XML contains a complex type that I created, as well as having a number of nested objects on which I have used the XMLARRAY / XMLARRAYITEM.

Fragment of test XML shown below...

<?xmlversion="1.0"encoding="utf-8"?>

<Testxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://tempuri.org">

<test1>201</test1>

<test2>Blah Blah</test2>

<test3>3</test3>

<test4>51</test4>

<TestTest>

<Inner>

<inner1>300</inner1>

<inner2>TEXT</inner2>

<inner3>TEXT</inner3>

<inner4>TEXT</inner4>

<inner5>TEXT</inner5>

<inner6>TEXT</inner6>

<inner7>TEXT</inner7>

<inner8>TEXT</inner8>

<inner9>TEXT</inner9>

<inner10>TEXT</inner10>

</Inner>

<Inner>

<inner1>301</inner1>

<inner2>TEXT</inner2>

<inner3>TEXT</inner3>

<inner4>TEXT</inner4>

<inner5>TEXT</inner5>

<inner6>TEXT</inner6>

<inner7>TEXT</inner7>

<inner8>TEXT</inner8>

<inner9>TEXT</inner9>

<inner10>TEXT</inner10>

</Inner> </TestTest>

</Test>

Now when I use the test harness the XML is generated exactly as I require.

Also I can reference the returned data on the client, for example by using arg.test1 would return '201'.

My probelms/question are these...

1> How do I access the inner array items. For example how would I access elements under TestTest?

2> How do I convert the returned data from the webservice into valid xml on the client? Every time I try and do this I get the error 'XML document must have a top level element'.

I cant understand why it says it is not valid as every other application I have has no problem with the XML that is generated,

Can anyone point me in the right direction as I cant see where the problem is.

Thanks in advance

Problem sortedSmile

Once I found out about the service using JSON by default it was very straightforward.

I looked at the JSON formatted data that was sent with the response. From there it was obvious how to call the array items.

For example arg.Level1[1].Level2Value

Didn't know much about JSON before this but it is a great format (compared to XML) to use when you want to handle stuff on the client in javascript. Even handles custom objects with ease.

Hope this helps somebody

XmlDocument parameter error

I have the following webservice function definition:

[WebMethod(EnableSession =true)]

[ScriptMethod(ResponseFormat =ResponseFormat.Xml)]

publicXmlDocument requestXML(XmlDocument requestXML)

I am trying to pass the XmlDocument parameter from JavaScript on an Ajax enabled web page:

<scripttype="text/javascript">

function newDocument(text)

{

if (typeof DOMParser !="undefined")

{

return (new DOMParser( )).parseFromString(text,"application/xml");

}

else

{

var doc =new ActiveXObject("MSXML2.DOMDocument");

doc.loadXML(text);

return doc;

}

}

// This function calls the Web Service method.

function request()

{

var entryElem = document.getElementById("entry");var entryText = newDocument("<loginOwner><question>"+entryElem.value+"</question><UserID>visitor</UserID><OwnerName>anonymous</OwnerName></loginOwner>");

improxy.requestXML(entryText, onOK);

}

function onOK(resultXML)

{

alert(resultXML.xml);

}

</script>

I am getting an error: "Microsoft JScript runtime error: Object doesn't support this action". In the debugger, this error occurs in the function: "Sys.Serialization.JavaScriptSerializer._serializeWithBuilder".

Isn't the XmlDocument type supported as a parameter in Ajax?

Thanks,

Eyal

The Client Side document is not compatible with the server side object. You can simply send the xml as plain string.


1. The return value is also a server side XmlDocument and this is translated correctly to the client side object. Can't this be done for a parameter?

2. If I send the XML as string, the webservice complains about wrong data types. Is there another way to tell the server to handle this string as XML?


Hi

I have tested that sending the XML as string to webservice,and it works.

Try this:

Default.aspx

<%@. Page Language="C#" %>

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

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>

<script type="text/javascript">
// function newDocument(text)
// {
// if (typeof DOMParser != "undefined")
// {
// return (new DOMParser( )).parseFromString(text, "application/xml");
// }
// else
// {
// var doc = new ActiveXObject("MSXML2.DOMDocument");
// doc.loadXML(text);
// return doc;
// }
// }

// This function calls the Web Service method.
function request()
{
// var entryElem = document.getElementById("entry");
var entryText = "<loginOwner><question>entryElem</question><UserID>visitor</UserID><OwnerName>anonymous</OwnerName></loginOwner>";
// throw "";
Samples.AspNet.WebService.GetXmlDocument(entryText, onOK);
}

function onOK(result)
{
// Page element to display feedback.
var RsltElem = document.getElementById("ResultId");

var readResult;
if (document.all)
readResult = result.documentElement.firstChild.text;
else
// Firefox
readResult = result.documentElement.firstChild.textContent;
RsltElem.innerHTML = "XmlDocument content: " + readResult;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="http://localhost/AJAXEnabledWebSite/WebService.asmx" />
</Services>
</asp:ScriptManager>
<div>
<button id="Button2" onclick="request(); return false;">
Server Time</button>
<span id="ResultId"></span>
</div>
</form>
</body>
</html>

WebService.asmx

<%@. WebService Language="C#" Class="Samples.AspNet.WebService" %>

using System;
using System.Web;
using System.Xml;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

namespace Samples.AspNet
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService() { }

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public XmlNode GetXmlDocument(string requestXML)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(requestXML);
return xmlDoc;
}
}
}

Best Regards


More information about this topic:

Passing an XML instance to a Web Service Method :http://objectsharp.com/cs/blogs/matt/archive/2004/10/30/989.aspx

Passing XmlDocument to .Net Webservice from IE client :http://www.dotnet247.com/247reference/msgs/6/32784.aspx


Thank you. I know passing a string works. My web service expects an XmlDocument parameter, not a string.


I tried the links. None of them shows how to pass an XmlDocument with Ajax. Only directly with XMLHTTP.