somehow i can't access wcf services via javascript. I have been following the instructions in this link and i get error that the service ojbect in javascript is undefined.
If you want to make a WCF service available to JavaScript, you can add endpoints to enable script in AJAX-enabled Web pages to access those services. For details you can find in
this
document.
In addtion, please refer a blog #Calling WCF Web Services from JavaScript, which point out the options for consuming a WCF web service from a web page.
I'm searching for a way to provide my current example solution folder, that i've written in Visual Studio 2010 .net4.0 c#. I think i've already enabled the ajax functionality for the endpoints. Look at this snapshot of my web.config.
Here is the codebehind of webservice CostService.svc.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
namespace SandwichServices
{
[ServiceContract(Namespace = "SandwichServices")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CostService
{
// Fügen Sie zum Verwenden von HTTP GET das Attribut [WebGet] hinzu. (Das standardmäßige ResponseFormat ist WebMessageFormat.Json.)
// Fügen Sie zum Erstellen eines Vorgangs, der XML zurückgibt,
// [WebGet(ResponseFormat=WebMessageFormat.Xml)] hinzu,
// und fügen Sie die folgende Zeile in den Vorgangstext ein:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public void DoWork()
{
// Hier die Vorgangsimplementierung einfügen.
return;
}
[OperationContract]
public double CostOfSandwiches(int quantity)
{
return 1.25 * quantity;
}
// Hier weitere Vorgänge hinzufügen und mit [OperationContract] kennzeichnen.
}
}
And the Default.aspx
<%@ Page Title="Startseite" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="SandwichServices._Default" %>
<script language="javascript" type="text/javascript">
// <![CDATA[
function Button1_onclick() {
var service = new SandwichServices.CostService();
service.CostOfSandwiches(3, onSuccess, null, null);
}
function onSuccess(result) {
alert(result);
}
// ]]>
</script>
<html>
<body>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/CostService.svc" />
</Services>
</asp:ScriptManager>
<input id="Button1" type="button" value="Price for 3 Sandwiches" onclick="return Button1_onclick()" /></p>
</form>
</body>
</html>
the issue was that the file CostService.svc had the name CostService .svc. There was a space between .svc and the last e. Readding the service resolved the issue. I also modified the javascript code into this, regarding the code on chandras page.
senseyyy
0 Points
6 Posts
Access WCF Services via Javascript
Nov 15, 2012 09:19 PM|LINK
Hi All,
somehow i can't access wcf services via javascript. I have been following the instructions in this link and i get error that the service ojbect in javascript is undefined.
Here is the link with the instructions
KR Omer
senseyyy
0 Points
6 Posts
Re: Access WCF Services via Javascript
Nov 18, 2012 03:26 PM|LINK
has anyone an idea how to access webservice with javascript?
Haixia Xie -...
Contributor
3022 Points
294 Posts
Microsoft
Re: Access WCF Services via Javascript
Nov 19, 2012 12:10 PM|LINK
Hi senseyyy,
If you want to make a WCF service available to JavaScript, you can add endpoints to enable script in AJAX-enabled Web pages to access those services. For details you can find in this document.
In addtion, please refer a blog #Calling WCF Web Services from JavaScript, which point out the options for consuming a WCF web service from a web page.
http://weblogs.asp.net/ricardoperes/archive/2011/03/29/calling-wcf-web-services-from-javascript.aspx
Best Regards.
Feedback to us
Develop and promote your apps in Windows Store
senseyyy
0 Points
6 Posts
Re: Access WCF Services via Javascript
Nov 19, 2012 01:48 PM|LINK
I'm searching for a way to provide my current example solution folder, that i've written in Visual Studio 2010 .net4.0 c#. I think i've already enabled the ajax functionality for the endpoints. Look at this snapshot of my web.config.
<system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="SandwichServices.CostServiceAspNetAjaxBehavior"> <enableWebScript /> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <services> <service name="SandwichServices.CostService"> <endpoint address="" behaviorConfiguration="SandwichServices.CostServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="SandwichServices.CostService" /> </service> </services> </system.serviceModel>Here is the file CostService.svc:
Here is the codebehind of webservice CostService.svc.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; using System.Text; namespace SandwichServices { [ServiceContract(Namespace = "SandwichServices")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class CostService { // Fügen Sie zum Verwenden von HTTP GET das Attribut [WebGet] hinzu. (Das standardmäßige ResponseFormat ist WebMessageFormat.Json.) // Fügen Sie zum Erstellen eines Vorgangs, der XML zurückgibt, // [WebGet(ResponseFormat=WebMessageFormat.Xml)] hinzu, // und fügen Sie die folgende Zeile in den Vorgangstext ein: // WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; [OperationContract] public void DoWork() { // Hier die Vorgangsimplementierung einfügen. return; } [OperationContract] public double CostOfSandwiches(int quantity) { return 1.25 * quantity; } // Hier weitere Vorgänge hinzufügen und mit [OperationContract] kennzeichnen. } }And the Default.aspx
<%@ Page Title="Startseite" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SandwichServices._Default" %> <script language="javascript" type="text/javascript"> // <![CDATA[ function Button1_onclick() { var service = new SandwichServices.CostService(); service.CostOfSandwiches(3, onSuccess, null, null); } function onSuccess(result) { alert(result); } // ]]> </script> <html> <body> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="~/CostService.svc" /> </Services> </asp:ScriptManager> <input id="Button1" type="button" value="Price for 3 Sandwiches" onclick="return Button1_onclick()" /></p> </form> </body> </html>oak_silver
Member
418 Points
64 Posts
Re: Access WCF Services via Javascript
Nov 20, 2012 12:33 AM|LINK
What issue have you encounter in this process? Post the detail error message here.
senseyyy
0 Points
6 Posts
Re: Access WCF Services via Javascript
Nov 20, 2012 07:47 AM|LINK
Runtime Error in Microsoft JScript: "SandwichServices" is undefined.
this error is raised from this line.
var service = new SandwichServices.CostService();
chandradev1
Participant
1561 Points
473 Posts
Re: Access WCF Services via Javascript
Nov 21, 2012 05:11 AM|LINK
Hi
You also check this URL
http://chandradev819.wordpress.com/2011/04/05/how-to-use-ajax-enabled-wcf-in-asp-net
My Blog
[Life is wonderful if you know how to live life]
senseyyy
0 Points
6 Posts
Re: Access WCF Services via Javascript
Nov 21, 2012 10:45 AM|LINK
Hi chandradev1 and all,
the issue was that the file CostService.svc had the name CostService .svc. There was a space between .svc and the last e. Readding the service resolved the issue. I also modified the javascript code into this, regarding the code on chandras page.
var service = new CostService();
service.CostOfSandwiches(3, onSuccess, null, null);
KR
omer