Json實現非同步請求(提交評論),json提交評論

來源:互聯網
上載者:User

Json實現非同步請求(提交評論),json提交評論

主要將代碼粘貼,通過閱讀代碼理解其中的相關邏輯。

html代碼:

<form id="form1" runat="server">    <p>        評論:</p>    <p>        姓名:<input type="text" name="username" id="username1" /></p>    <p>        內容:<textarea name="content" id="content" rows="2" cols="20"></textarea></p>    <p>        <input type="button" id="send" value="提交" /></p>    </form>    <div class="comment">        已有評論:</div>    <div id="resText">    </div>

js代碼:

            $("#send").click(function () {                $.get("doSave.ashx", {<span style="white-space:pre"></span>  <span style="font-family: Arial, Helvetica, sans-serif;"></span>//調用json外掛程式                    u_name: $("#username1").val(),       //json資料/值對化                    u_cont: $("#content").val()                }, function (data)                     var uName = data.username;       //註:此處的username與doSave.ashx中的dic.add("username",uname)中的username相對應的                    var uCont = data.content;                    var txtHtml = "<div class='comment'><h6>"                                   + uName + ":</h6><p class='para'>"                                   + uCont + "</p></div>"                    $("#resText").html(txtHtml);  //將返回的資料添加到頁面上                }, "json");            })

外掛程式代碼:

<%@ WebHandler Language="C#" Class="doSave" %>using System;using System.Web;public class doSave : IHttpHandler{    public void ProcessRequest(HttpContext context)    {        var dic = new System.Collections.Generic.Dictionary<string, object>();    //儲存的集合        string jsonStr = "{}";                     //建立字串jsonStr        context.Response.ContentType = "text/json";          //定義返回的內容類型為json        string uname = context.Request.QueryString[0];       //擷取請求參數中第一個參數,也可以直接使用uname        string commet = context.Request.QueryString[1];      //定義字串uname、commet為context請求查詢的字串context.Request.Params["username"];QyertStrubg:查詢字串        dic.Add("username", uname);                      //將字串添加到對象中        dic.Add("content", commet);        jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(dic);     //序列化集合為json字串        context.Response.Write(jsonStr);    }    public bool IsReusable    {        get        {            return false;        }    }}


此處效果即為,在輸入框中輸入相關文字,點擊提交,下方會自動將書寫的文字進行展示,無需跳轉其他頁面。



ajax怎實現提交表單是的非同步

<input type="text" name="username" id="username" >
<input type="button" value="測試" onclick="test();">
<script type="text/javascript">
get $.ajax 還有這2種方式 我用了個post作為例子
function test()
{
//這個是jquer的 請求 你要載入個jquery的庫 舉例是post請求
$.post(
'這裡是你請求的地址',
{
//你傳的參數 把你的input框的值以post方式傳送
username:$('#username').val()
},
function(data)
{
這裡是從你請求的地址返回來的資料 具體怎麼處理就看你自己的需求了
})
}
</script>
 
liger ui怎把非同步獲得json資料賦值給一個全域變數

var data;//給這個全域變數賦值
$.ajax({
type : 'post',
cache : false,
url : 'scoredeal/score_info!doEnterScoreInf.action',
success : function(data2) {

data=data2;
alert(data2);//json字串已經獲得
return;
}

});

試一下吧,我猜的未驗證通過。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.