jQuery는 RESTful WCF 샘플 코드를 호출합니다(GET 메서드/POST 메서드)_jquery

WBOY
풀어 주다: 2016-05-16 17:02:23
원래의
1166명이 탐색했습니다.

더 이상 헛소리하지 말고 바로 본론으로 들어가겠습니다

wcf 끝:

최근 Ajax 호출을 활성화하고 Restful 스타일 URI를 지원하려면 Ajax 지원 Wcf 서비스를 생성한 후 svc 파일을 수동으로 수정하고 Factory를 지정해야 합니다. 즉, 다음과 같이 Restful이 더욱 인기를 얻고 있습니다.

<%@ ServiceHost Language="C#" Debug="true" Service="ajaxSample.HelloWorld" CodeBehind="HelloWorld.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

참고: Factory가 추가되지 않으면 http://localhost/helloWorld.svc/Hello/person/name과 같은 편안한 방법을 사용하여 wcf에 직접 액세스할 수 없습니다.

동시에 web.config에서 를 제거합니다.


                                                🎜>

 ;/behavior>

"
multipleSiteBindingsEnabled="true" /> 바인딩="webHttpBinding" contract="ajaxSample.HelloWorld" />



자, 이제 wcf를 호출할 때 GET/POST 메소드가 두 가지 있으므로 일반적으로 사용되는 여러 상황에 대한 예제 메소드를 작성해 보겠습니다.




복사 코드

코드는 다음과 같습니다.


System.Collections.Generic 사용;
System.ServiceModel 사용;
System.ServiceModel.Activation 사용;
System.ServiceModel.Web 사용;

네임스페이스 ajaxSample
{
    [ServiceContract(Namespace = "http://yjmyzz.cnblogs.com/")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
공개 클래스 HelloWorld
    {

        ///
        /// 只能Post的Restful방법
        ///

        ///
        ///
        /// <반품>
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "PostRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
        public List PostRestfulTest(문자열 사람,문자열 환영)
        {
            List 결과 = 새로운 List();

            result.Add("PostRestfulTest -> 서버에서:");
            결과.추가(사람);
            결과.추가(환영);
            결과 반환;
        }

        /// <요약>
        /// 只能Get的Restful방법
        ///
        ///
        ///
        /// <반품>
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "GETRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
        public List GETRestfulTest(문자열 사람, 문자열 환영)
        {
            List 결과 = 새로운 List();

            result.Add("GETRestfulTest -> 서버에서:");
            결과.추가(사람);
            결과.추가(환영);
            결과 반환;
        }

        /// <요약>
        /// 即可Get与PostfulRestful방법
        ///
        ///
        ///
        /// <반품>
        [OperationContract]
        [WebInvoke(Method = "*", UriTemplate = "RestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]
        public List RestfulTest(string person, string Welcome)
        {
            List result = new List();

           result.Add("RestfulTest -> from server:")
           result.Add(person);
           result.Add(환영)           결과 반환;

 
        ///
        /// 只能Post的常规方法(注:Post方式,BodyStyle必须设置WrappedRequest或Wrapped)
        ///

        ///
        ///
        /// < return
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]
        public List , 문자열 환영)
        {
            List result = new List();

            result.Add("PostRestfulTest -> 서버에서:");
            결과.추가(사람);
            결과.추가(환영);
            결과 반환;
        }

        /// <요약>
        /// 只能Get 常规方法
        ///
        ///
        ///
        /// <반품>
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
        public List GETTest(문자열 사람, 문자열 환영)
        {
            List 결과 = 새로운 List();

            result.Add("GETTest -> 서버에서:");
            결과.추가(사람);
            결과.추가(환영);
            결과 반환;
       }

         

         
    }
}


jQuery调用代码:


复主代码 代码如下:


   

<본문>
   

       
   





示例代码:

点击下载 
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!