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

问题描述:

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

WCF侧VS 2010:

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

HTML中的Aptana工作室:

enter image description here

代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/latest/jquery.js"></script> 

$(document).ready(function() { 
    $("#sayHelloButton").click(function(){ 
     alert("fsf"); 



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

      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function(msg) { 
       AjaxSucceeded(msg); 
      }, 
      error: AjaxFailed 
     }); 
    }); 
}); 
     function AjaxSucceeded(result) { 
      alert(result.d); 
     } 
     function AjaxFailed(result) { 
      alert(result.status + ' ' + result.statusText); 
     } 

测试

点击我

首页

Contact

<div> 

</div> 

<footer> 
<p>&copy; Copyright by yusufkaratoprak</p> 
</footer> 

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

步骤1.装饰GetData方法与WebInvoke属性。

[WebInvoke(Method="POST", UriTemplate="/GetData", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)] 
    public string GetData(int value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 

第2步。通过配置或通过或通过代码将WebHttpBehavior添加到端点。

ServiceEndpoint ep = //your hosting endpoint   
      //Add behaviors to the endpoint 
      ep.EndpointBehaviors.Add(new WebHttpBehavior()); 

第3步。在$ .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 
     }); 

您的网址错误。如果这是你的本地网络服务器,它可能像

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