码迷,mamicode.com
首页 > Web开发 > 详细

ajax调用本地wcf中的post和get

时间:2015-06-02 00:05:36      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

我们可以通过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提交方式造成的。

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

 

ajax调用本地wcf中的post和get

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!