日期:2014-05-17  浏览次数:20731 次

php使用ajax调用回调函数并传值,回调函数接收不到值
var _RUIKA_USER_LOGIN_API="http://user.test.com/api.ajaxLogin.do";

//前台Ajax执行登录
function rk_login(username, password) {
var result = false;
if (username != null && $.trim(username).length > 0
&& password != null && $.trim(password).length > 0) {
$.ajax({
type : "get",
url : _RUIKA_USER_LOGIN_API,
async : true,
data : {
"username" : username,
"password" : password
},
dataType : "jsonp",
jsonp : "callbackparam",
jsonpCallback : "rk_login_callBack"
});
}
return result;
}

////////////www.ruika365.com/index.html使用

//执行登录
$("#index_login_submit").click(function(){
var username = $("#login_info").val();
var password = $("#password").val();
var a = rk_login(username,password);
                return false;
});
});
//java那里已经在后台输出
//rk_login_callBack({type:1,id:"51be591d2a1d4f6c86f3f606aa1cab39",name:"admin",headpic:"http://user.test.com/front/head/20130914103019234.jpg"})
// ajax 登录成功后回调函数
function rk_login_callBack(data) {
        alert(data);//这里没有data为空,如何获取这里的值 var type = data.type;
var name = data.name;
        var id = data.id;
        var headpic = data.headpic;
if (type==1) {
            document.getElementById("login_type").value=1;     
}else{
            document.getElementById("login_type").value=0;
            $("#error_message").show();
        }
}
ajax jquery 回调函数

------解决方案--------------------
从后到前一步步查,别急。

1、查java是否有返回值,返回值是否正确。
2、在浏览器network(ie里是网络)看你ajax请求返回的是什么。
3、如果上面两条都没问题,那就看ajax方法调用的对不对。
------解决方案--------------------
按照描述数据是按照正确格式到你前台了,那么可以这样,不要用jquery的ajax,用js标准写法试试看能不能得到值。
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",_RUIKA_USER_LOGIN_API+"?username="+username+"&password="+password,true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
alert(xmlhttp.responseText);