Javascript 如何在jQueryHTML应用程序中使用wcf服务作为远程或本地?

Javascript 如何在jQueryHTML应用程序中使用wcf服务作为远程或本地?,javascript,jquery,html,wcf,web-services,Javascript,Jquery,Html,Wcf,Web Services,嗨;如何在jQueryHTML应用程序中使用wcf服务作为远程或本地?我已经准备好了wcf服务。我想在html文件中使用jQueryAjax方法。任何错误都不会返回 与2010年相比,WCF方面: public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } apta

嗨;如何在jQueryHTML应用程序中使用wcf服务作为远程或本地?我已经准备好了wcf服务。我想在html文件中使用jQueryAjax方法。任何错误都不会返回

与2010年相比,WCF方面:

    public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
aptana studio中的HTML:

代码:

测试 点击我 家


&抄袭;yusufkaratoprak版权所有


您的URL错误。如果它在您的本地Web服务器上,则可能类似于

"http://localhost/service1.svc/getdata"

在.CS文件中需要执行几个步骤-

第一步。用
WebInvoke
属性修饰GetData方法

[WebInvoke(Method="POST", UriTemplate="/GetData", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
第二步。通过配置或代码向端点添加WebHttpBehavior

ServiceEndpoint ep = //your hosting endpoint          
            //Add behaviors to the endpoint
            ep.EndpointBehaviors.Add(new WebHttpBehavior());
第三步。在$.ajax方法中,使用服务的完整url和方法名

$.ajax({
             type: "POST",
             url: "http://localhost/Service1.svc/GetData",
             data: "{'id': '" + 1 + "'}",

             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function(msg) {
                 AjaxSucceeded(msg);
             },
             error: AjaxFailed
         });
[WebInvoke(Method="POST", UriTemplate="/GetData", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
ServiceEndpoint ep = //your hosting endpoint          
            //Add behaviors to the endpoint
            ep.EndpointBehaviors.Add(new WebHttpBehavior());
$.ajax({
             type: "POST",
             url: "http://localhost/Service1.svc/GetData",
             data: "{'id': '" + 1 + "'}",

             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function(msg) {
                 AjaxSucceeded(msg);
             },
             error: AjaxFailed
         });