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提交方式造成的。

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

  • 相关阅读:
    MapReduce-shuffle过程详解
    YARN中的失败分析
    HBase协处理器的使用(添加Solr二级索引)
    Flume具体应用(多案例)
    Flume架构及运行机制
    python Cmd实例之网络爬虫应用
    mongodb3 权限认证问题总结
    webpack配置
    apt软件包管理
    python笔记之编程风格大比拼
  • 原文地址:https://www.cnblogs.com/zzPrince/p/4545118.html
Copyright © 2011-2022 走看看