Ajax向后台發送數據


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);
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM