zoukankan      html  css  js  c++  java
  • 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提交方式造成的。

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

  • 相关阅读:
    进程的Binder线程池工作过程
    Binder系列—开篇
    shell脚本使用技巧3--调试
    shell脚本使用--sleep
    shell脚本使用技巧2
    linux添加环境变量
    shell脚本学习1(Linux脚本攻略)
    c++语言的设计和演化---在线函数
    vim常用快捷键
    git常用命令
  • 原文地址:https://www.cnblogs.com/zzPrince/p/4545118.html
Copyright © 2011-2022 走看看