获取ajax返回值的问题

一、前端代码

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

}
 }

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...