本文介绍了使用$ .ajax函数调用wcf rest服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我是wcf休息服务的新手。我创建了两个方法,如Welcome和Hello。这两种方法在浏览器中工作,如http:// localhost:14502 / MyRestService.svc / Hello / xxx 和http:// localhost:14502 / MyRestService.svc / Welcome。但是当我使用jquery在asp.net应用程序中使用该服务时。它给出了null结果。请给出解决方案。I am new to wcf rest services. I have created two method like Welcome and Hello. The two methods are working working in the browser like http://localhost:14502/MyRestService.svc/Hello/xxx"and http://localhost:14502/MyRestService.svc/Welcome. But when i was consuming the service in the asp.net application by using jquery. It gives null result. Please give resolve that.[ServiceContract] public interface IMyRestService { [OperationContract] [WebGet( RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Welcome",BodyStyle=WebMessageBodyStyle.Wrapped)] string Welcome(); [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "Hello/{name}",BodyStyle=WebMessageBodyStyle.Wrapped)] string Hello(string name); [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Test?id={id}")] int Test(int id); } public class MyRestService : IMyRestService { public string Hello(string name) { return "Hello " + name; } public int Test(int id) { return id + 1; } public string Welcome() { return "Welcome to Rest Services"; } } web.config文件是The web.config file is<?xml version="1.0"?><configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service name="RestService.MyRestService" behaviorConfiguration="ServiceBehaviour"> <!-- end point--> <endpoint address="" binding="webHttpBinding" contract="RestService.IMyRestService" bindingConfiguration="webHttpBindingJsonP" behaviorConfiguration="web"></endpoint> <host> <baseAddresses> <add baseAddress="http://localhost:8001"/> </baseAddresses> </host> </service> <!----> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehaviour"> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="123456" /> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="web"> <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="123456"/> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <bindings> <webHttpBinding> <binding name="webHttpBindingJsonP" crossDomainScriptAccessEnabled="true"/> </webHttpBinding> </bindings> <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer></configuration> Asp.net网页是The Asp.net web page is<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConsumingWCFRestService.aspx.cs" Inherits="ConsumingWCFRestService" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $("#btnSubmit").click(function () { var url = "http://localhost:14502/MyRestService.svc/Welcome"; $.getJSON(url, null, function (data) { if (data != null) { alert("success"); } else { alert(data); } }); }); }); </script></head><body> <form id="form1" runat="server"> <div> <input type="submit" id="btnSubmit" value="Call Rest" /> </div> </form></body></html> 请有人解决并回复我...Please anybody resolve and reply to me...推荐答案 请有人解决并回复我......Please anybody resolve and reply to me... 这篇关于使用$ .ajax函数调用wcf rest服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 10:50
查看更多