必学习的前后端交互框架ajax

ajax显然是最重要的框架

无论是c#,java,web程序通通能够解决前后端问题。

现在越来越多的人能够开发,为什么?

框架已经改变了程序员,现在基本是个程序员都可以在一个星期内写一个web程序。

ajax让他们更快的上手,mvc框架让他们灵活运用。

ajax替代了以往的servlet,让主界面不用跳转就可以得到不同数据的显示。

ajax有这独特的意义,

ajax如何使用,非常简单,以下步骤就可以学习ajax。

下载ajax.dll

引入项目,

配置web文件

新建ajax.cs文件,注册

html引用和control调用基本事例

function click1() {
        @*location.href = "getresult?xu="+xu;*@
        //$(#cc).append(<h1>fdghhhhhhhhhhhhhh</h1>);
        //var tImg = "<div id=dfggdfgd>sfrgdfgdfhfghgf</div>";
        // alert();
        $(body).append("<p>sfdgfdfgfdddddddd</p>");
        $(p).remove();
        $.ajax({
            type: post,
            url: @Url.Action("Contact"),
            data: {xu:677},
            contentType: application/json;charset=utf-8,
            dataType: json,
            success: function (message) {

                alert(message.message);
            }

        });

    }

定义事件,实现ajax传输数据和处理数据

后端

[HttpPost()]
        public ActionResult Contact()
        {
            JsonResult ajaxres = new JsonResult();
            ajaxres.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            //ViewData["xu"] = xu;
            ajaxres.Data = new { message = "徐敬坤" ,data1="67"};
           // String data1 = "67876877778";
            return ajaxres;
        }

这样实现前后端交互

实现这必须要有ajax.dll文件

除了ajax还有form表单,但是处理原理是一致的

<form id="ff" method="post" action="@Url.Action("login1")">
        <center>
            <div class="easyui-panel" style="width:400px;padding:50px 60px">
                <div style="margin-bottom:20px">
                    <input class="easyui-textbox" prompt="Username" iconWidth="28" style="width:100%;height:34px;padding:10px;">
                </div>
                <div style="margin-bottom:20px">
                    <input class="easyui-passwordbox" prompt="Password" iconWidth="28" style="width:100%;height:34px;padding:10px">
                </div>
            </div>
            <input type="submit" value="login" />
            <input type="button" value="register" onclick="click1()" />
        </center>
    </form>

都是通过url.action实现跳转后端。

经验分享 程序员 微信小程序 职场和发展