ajax在success中location无法跳转页面问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Demo_Liu/article/details/81388809

我在使用ajax的过程中,当返回成功信息的时候在success:function()跳转另一个页面使用了window.location("url")来跳转页面,出现了页面无法跳转的问题

下面是解决方法:

$.ajax({
    		url:"http://...",
    		dataType:"jsonp",
    		data:{"fr_username":"admin","fr_password":"123456"},  
    		jsonp:"callback",   
    		timeout:5000,
    		success:function(data){
    			if(data.status==="success"){    				
    				$(window).attr("location","http://...");
    			}else if(data.status === "fail"){  
    				alert("用户名密码错误!!!");
    			}
    		},
    		error:function(){   
    			alert("超时或服务器其他错误!!!");
    			}   
    	});

因为location是js的原生跳转的方法,而ajax是经过封装的jQuery,所以我们要用jQuery中的跳转方法才能生效

我们在success:function()中要使用$(window).attr("location","url")的方式跳转页面;

以上

 

参考资料:https://blog.csdn.net/open_yu/article/details/77933757

猜你喜欢

转载自blog.csdn.net/Demo_Liu/article/details/81388809