加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

获取ajax返回值的问题

发布时间:2020-12-16 02:05:34 所属栏目:百科 来源:网络整理
导读:一、前端代码 htmlheadtitlebian/titlemeta charset="utf-8" /script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"/scriptscript type="text/javascript"function check(){var aData=testAsync();//aData={"login":"1","message":"sucessful"}

一、前端代码

<html>
<head>
	<title>bian</title>
	<meta charset="utf-8" />
	<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
	<script type="text/javascript">
		function check(){
			var aData=testAsync();//aData={"login":"1","message":"sucessful"}
			if(aData.login==1){
				$('#message').css({display:'block'});
				$('.error').html('用户名存在');				

			}
		}

		function testAsync() {
		    //定义一个全局变量来接受$post的返回值
		    var result;
		    //用ajax的“同步方式”调用一般处理程序
		    $.ajax({
		        url: "<{$URL1}>/example/perfdata/format/json",//url: "/project/CI/index.php/example/perfdata/format/json",async: false,//改为同步方式
		        type: "post",success: function (DataValue) {
		            result = DataValue;
		        }
		    });
		    return result;
		}
    </script>

</head>
<body>
	
<h1>ajax异步调用演示</h1>


	<button  type="button" onclick="check()">异步调用</button>
    <div id="message" style="display:none">
    <p class="error"></p>


</body>
</html>



二、后端代码

1、restful接口

<?php defined('BASEPATH') OR exit('No direct script access allowed');


// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Example extends REST_Controller
{
public function perfdata_post(){
    $arr = array('login'=>'1','message'=>'sucessful');
   $this->response($arr,200);

}
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读