ajax调用本地wcf中的post和get

我们可以通过jQuery调用本地或者远程的wcf服务,本文讲解的是对本地wcf服务的post和get调用方式。

post和get到底有什么区别呢?此处不作详述。

但是,post对请求的数据格式更为严格。

如有一个方法login如下:

[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
         string login(string userEmail, string userPasswd);

 当使用get请求时,

 $.ajax({
                type: 'get',
                url: '/Service1.svc/login',
                contentType: 'text/json',
                data: { userEmail: "12345", userPasswd: "2334" },       // OK  
               //data: { "userEmail": "12345", "userPasswd": "2334" },  //OK  
               //data: '{"userEmail":"12345","title":"2334"}',          //OK  
                success: function (msg) {
                    alert(msg);
                },
                error: function (msg)
                {
                    alert(msg);
                }
            });

 当时当使用post请求时:

首先,我们需要将wcf中的方法去掉Method = "GET"属性,在前端调用时

 $.ajax({
                type: 'get',
                url: '/Service1.svc/login',
                contentType: 'text/json',
                data: { userEmail: "12345", userPasswd: "2334" },        // OK              
           //data: { "userEmail": "12345", "userPasswd": "2334" },  //Error             
           //data: '{"userEmail":"12345","title":"2334"}',         //Error            
                success: function (msg) {
                    alert(msg);
                },
                error: function (msg)
                {
                    alert(msg);
                }
            }); 

 不难发现,post对数据的请求有着更为严格的要求。这归根结底是由于http几种不同form提交方式造成的。

记录于此,一为记录点滴,另则为分享所获。

原文地址:https://www.cnblogs.com/zzPrince/p/4545118.html