PHP+JQuery实现ajax跨域
生活随笔
收集整理的這篇文章主要介紹了
PHP+JQuery实现ajax跨域
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
jQuery實現ajax跨域
1、dataType:'jsonp'
2、type: 'get'
3、把要傳的參數以url方式傳出去? url:'http://gameapi.feiliu.com/lqzg?tel='+telnumber,
JS代碼
<script> $.ajax({url:'http://gameapi.feiliu.com/lqzg?tel='+telnumber,async: true, type:"get",dataType:'jsonp',jsonp:"jsoncallback",data:{telnumber:telnumber},success:function(data,status){if(data.key == 1){$('#gacode').text('驗證碼為:'+data.mes).css('display', 'block');}else if(data.key == -1){$('#gacode').text('出錯了!'+data.mes).css('display', 'block');}else{alert('沒有任何東西!');}},error:function(){alert(arguments[1]);} }); </script>php代碼(CI框架)
public function index(){ header('Content-Type:text/html;Charset=utf-8'); $tel = $_GET['telnumber'];if(! preg_match("/1[3458]{1}\d{9}$/",$tel)){$key = -1;$mes = '手機號無效!';}else{//檢查號碼是否第一次領取if( $this->gamem->test_phone($tel)){$key = -1;$mes = '手機號已經領取過!';}else{//隨機選取一個code$code = $this->gamem->get_code(); if($code){//將該用戶信息插入$ret = $this->gamem->insert_user($tel);if($ret){$key = 1;$mes = $code;}}}} $data = array('key' => $key,'mes' => $mes);echo $_GET['jsoncallback'] . "(".json_encode($data).")"; exit;}?
轉載于:https://www.cnblogs.com/hejun695/p/5231666.html
總結
以上是生活随笔為你收集整理的PHP+JQuery实现ajax跨域的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML5 Web SQL
- 下一篇: NMF和SVD在推荐系统中的应用(实战)