我使用cordova通过简单的登录表单来建立移动应用程序。问题是每当我输入错误的用户名和密码时,我都能够从服务器获得XML中的无效信息。 但是,当我输入有效的用户名和密码时,我在 XHR加载失败时登录控制台:POST 。
有人可以帮我解决这个问题吗?
$(document).ready(function(){
$("#login").click(function(){
$.ajax({
url: "http://remoteServerIP/login.do",
data:{
password:$('#pass').val(),
method:"login",
userName:$('#uname').val(),
cType:"LOGIN"
},
type:'post',
dataType: 'xml',
async: true,
contentType:"application/x-www-form-urlencoded",
success: function(result){
alert("Data: "+result.data);
},
error: function(xhr, ajaxOptions, thrownError){
alert("msg: "+thrownError.message+" , status: "+xhr.status);
}
});
});
});
config.xml: -
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.MyApp.App" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<preference name = "SplashScreen" value = "screen" />
<preference name = "SplashScreenDelay" value = "4000" />
<preference name = "SplashMaintainAspectRatio" value = "true" />
<preference name="Orientation" value="portrait"/>
<name>DikshaTouch</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<!-- <access origin="*" /> -->
<access origin="http://remoteServerIP" subdomain="true" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
<icon src="www/img/icon.png" density="ldpi" />
<icon src="www/img/icon.png" density="mdpi" />
<icon src="www/img/icon.png" density="hdpi" />
<icon src="www/img/icon.png" density="xhdpi" />
<icon src="www/img/icon.png" density="xxhdpi" />
<icon src="www/img/icon.png" density="xxxhdpi" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
</widget>`
答案 0 :(得分:0)
我刚解决了这个问题。 问题是我正在进行异步调用,这就是为什么它是调试日志,因为 XHR加载失败:POST 当我将虚拟数据发送到服务器时。 我刚刚将 async:true 更改为 async:false
这是我改变的代码: -
$(document).ready(function(){
$("#login").click(function(){
$.ajax({
url: "http://remoteServerIP/login.do",
data:{
password:$('#pass').val(),
method:"login",
userName:$('#uname').val(),
cType:"LOGIN"
},
type:'post',
dataType: 'xml',
async: false,
contentType:"application/x-www-form-urlencoded",
success: function(result){
alert("Data: "+result.data);
},
error: function(xhr, ajaxOptions, thrownError){
alert("msg: "+thrownError.message+" , status: "+xhr.status);
}
});
});
});
现在我从服务器获得所需的响应..