• js 调用接口并传参


    注:需先引入 jquery.json-xx.min.js

    1. 参数跟在url后面

    var name = '王一';
    var age = 18;
    $.ajax({
        type : 'get',
        url : "xxxxxx?name="+name+"&age="+age,
        async : false,//同步/异步
        contentType : "application/x-www-form-urlencoded; charset=gbk",
        dataType : 'json', //返回 JSON 数据
        beforeSend : function() { //调用前触发,如加载效果等
            show('.load'); 
        },
        success : function(data, status) {
            var rstate = data.result;
            if (rstate == "0") {
                alert('接口调用成功!');
            } else {
                alert('接口调用失败!');
            }
        },
        complete : function() { //调用后触发(不管成功或失败)
            hide('.load);
        },
        error : function(data, status, e) {
            alert('接口调用错误!');
        }
    });

    2. 参数在data内

    $.ajax({
        type : 'get',
        url : 'xxxxx',
        async : false,
        contentType : "application/x-www-form-urlencoded; charset=gbk",
        data : {
            'name': '王一', //json格式
            'age': '18'
        },
        dataType : 'json',
        beforeSend : function() { 
            show('.load'); 
        },
        success : function(data, status) {
            var rstate = data.result;
            if (rstate == "0") {
                if (rstate == "0") {
                alert('接口调用成功!');
            } else {
                alert('接口调用失败!');
            }
        },
        complete : function() { 
            hide('.load);
        },
        error : function(data, status, e) {
            alert('接口调用错误!');
        }
    });    

    3. param传参

    var obj = new Object();
    obj.name = '王一';
    obj.age = 18;
    $.ajax({
        type : 'post',//也可为get
        url : 'xxxxx',
        async : false,
        contentType : "application/x-www-form-urlencoded; charset=gbk",
        data : {
            param : $.toJSON(obj) //转换为json格式
        },
        dataType : 'json',
        beforeSend : function() { 
            show('.load'); 
        },
        success : function(data, status) {
            var rstate = data.result;
            if (rstate == "0") {
                if (rstate == "0") {
                alert('接口调用成功!');
            } else {
                alert('接口调用失败!');
            }
        },
        complete : function() { 
            hide('.load);
        },
        error : function(data, status, e) {
            alert('接口调用错误!');
        }
    });
  • 相关阅读:
    Send EMail from your .NET Application using your GMail Account
    ObjectContext 是开发人员在查询、添加和删除其实体实例以及将新状态保存回数据库时用到的主要构造
    AjaxPro使用说明
    .NET Framework 类库
    sysobjects syscolumns和SysTypes笔记
    javascript 懒加载技术(lazyload)简单实现
    WEB前端开发笔试题2
    css方框模型(盒模型Box Model)
    让IE支持CSS3选择器的方法(利用JS)
    让IE6/IE7/IE8浏览器支持CSS3属性
  • 原文地址:https://www.cnblogs.com/linjiangxian/p/11465911.html
Copyright © 2020-2023  润新知