Javascript 电话缺口表单未提交到服务器
我有这张表格Javascript 电话缺口表单未提交到服务器,javascript,forms,cordova,Javascript,Forms,Cordova,我有这张表格 商户名称 商户密码 登录 这是它的js $("#login_form").submit(function(e) { $("#loading_div").show(); var inputs = []; var res = true; inputs[0] = document.getElementById("login_merchant_name"); inputs[1] = document.getElementById("login_
商户名称
商户密码
登录
这是它的js
$("#login_form").submit(function(e)
{
$("#loading_div").show();
var inputs = [];
var res = true;
inputs[0] = document.getElementById("login_merchant_name");
inputs[1] = document.getElementById("login_merchant_password");
for (i = 0; i < inputs.length; i++) {
if (inputs[i].value === "")
{
inputs[i].style.background = "#ffcccc";
res = false;
$("#loading_div").hide();
toastr.error(inputs[i].name + ' must not be empty');
} else
{
inputs[i].style.background = "#fff";
}
}
if (res === true)
{
data = $(this).serialize();
$.ajax({
type: "POST",
crossDomain: true,
timeout: 5000,
contentType: "application/json; charset=utf-8",
dataType: 'jsonp',
cache: false,
url: "http://gbelo.com/app/process.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
if(data.status === 'success'){
$("#loading_div").hide();
toastr.success('Login successful');
var merchant_id = data.merchant_id;
alert(merchant_id);
setTimeout(function() {
window.top.location.href='home.html';
}, 2000);
}else if(data.status === 'incorrect password'){
$("#loading_div").hide();
toastr.error('Incorrect password');
}else if(data.status === 'name does not exist'){
$("#loading_div").hide();
toastr.error('invalid merchant name');
}
}
});
}
e.preventDefault();
});
$(“#登录表格”)。提交(功能(e)
{
$(“加载分区”).show();
var输入=[];
var res=真;
输入[0]=document.getElementById(“登录名”);
输入[1]=document.getElementById(“登录\商户\密码”);
对于(i=0;i
表单验证工作得很好,它倾向于提交,但是它没有从服务器得到任何响应。我尝试过使用相同表单的服务器端代码,它可以工作,但似乎无法使用phone gap
请看一看我的代码并指出我的错误。好的,首先你必须将它添加到你的
res/xml/config.xml
文件中,然后在你的php文件中,在所有
头('Access-Control-Allow-Origin:')之前添加它;
标题('Access-Control-Allow-Methods:GET,POST')代码>
更新
安装此cordova插件org.apache.cordova.whitelist
cordova插件添加org.apache.cordova.whitelist
然后将其添加到config.xml
文件中
<allow-navigation href="http://*/*" />
尝试此ajax请求您是否在google chrome中进行了测试,如果出现任何错误消息,请查看开发控制台控制台控制台仅显示这些错误。1) 得到file:///Users/Cashmere/Documents/Phonegap-projects/Gbelo!/www/js/jquery.simple-text-rotator.js net::ERR_FILE_NOT_FOUND login.html:55 2)GETfile:///Users/Cashmere/Documents/Phonegap-projects/Gbelo!/www/cordova.js net::ERR_FILE_NOT_FOUND 2login.html:1 3)跨源资源共享策略已阻止加载来自源“”的字体:请求的资源上不存在“访问控制允许源”标题。因此,不允许访问源“null”。所有这些都不能阻止它工作,所以这是一个CORS问题,让我发布一个答案,它已经在配置文件中了。我将代码添加到php文件中,但仍然不起作用。您是否直接在android手机上试用了该应用程序?耶。它只显示加载div及其应该删除的内容,当成功/错误响应出现时,请将其安装好,并将代码保存在xml中,但不工作,仍然没有响应。甚至只是为了发送成功报告而更改了php文件。if(isset($_POST[“login_merchant_name”]){$response['status']=“success”;echo json_encode($response);}
<allow-navigation href="http://*/*" />
$.ajax({
url: "http://gbelo.com/app/process.php",
type: 'POST',
data: data,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
success: function(data, textStatus, jqXHR){
// handle success
console.log("Hooray, it worked!");
},
error: function(jqXHR, textStatus, errorThrown){
// handle error
console.error("The following error occurred: "+textStatus, errorThrown);
}
});