Ajax向後台發送資料

來源:互聯網
上載者:User

標籤:console   json   ready   標籤   attr   tar   ati   func   doc   

Ajax向後台發送資料,三種情況:

1:Ajax手動發資料

#GET  發資料
<div> <a class="btn" onclick="AjaxSubmit1();">點我</a> </div><script src="/static/js/jquery-3.2.0.js"></script> <script> function AjaxSubmit1() { $.ajax({ url:‘/ajax1.html‘, type:‘GET‘, data:{‘p‘:123}, success:function (arg) { } }) } </script>
 
#POST 發送資料
function AjaxSubmit3() { $.ajax({ url:‘/ajax1.html‘, type:‘POST‘, data:{‘p‘:123}, success:function (arg) { } }) }

 

2:HMLHttpRequest方法

#get發送資料

function AjaxSubmit2() { var xhr=new XMLHttpRequest(); xhr.onreadystatechange=function () { if(xhr.readyState==4){ //接受完畢伺服器返回的資料 console.log(xhr.responseText); } }; xhr.open(‘GET‘,‘/ajax1.html?p=123‘); xhr.send(‘null‘); }
#POST發送資料

function AjaxSubmit4() { var xhr=new XMLHttpRequest(); xhr.onreadystatechange=function () { if(xhr.readyState==4){ //接受完畢伺服器返回的資料 console.log(xhr.responseText); } }; xhr.open(‘POST‘,‘/ajax1.html‘); xhr.setRequestHeader(‘Content-Type‘,‘application/x-www-form-urlencoded;charset-UTF-8‘) xhr.send(‘p=456‘); }

 

3:Iframe標籤+Form表單(‘偽’Ajax)

 <h6>基於iframe+Form表單</h6>        <iframe id=‘iframe‘ name="ifra"></iframe>        <form id=‘fm‘ action="/ajax1.html" method="POST" target="ifra">            <input name="root" value="111"/>            <a onclick="AjaxSubmit5();">提交</a>function reloadIframe() {{#            this.contentWindow#}            var content=this.contentWindow.document.body.innerHTML;            var obj=JSON.parse(content);            if(obj.status){                alert(obj.message);            }        }

 

 

 

ps:基於Iframe發送資料

<h6>學習iframe</h6>        <div>            <input id="url" placeholder="請輸入URL"/><a onclick="Test1();">查看</a>        </div>        <iframe id=‘iframe‘ style="height: 800px;width: 600px;" src="http://www.autohome.com.cn"></iframe> function Test1() {            var url=$(‘#url‘).val();            $(‘#iframe‘).attr(‘src‘,url);        }

 

Ajax向後台發送資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.