connect_error); } -6ren">
gpt4 book ai didi

php - ionic 2 与 php mysql

转载 作者:行者123 更新时间:2023-11-29 20:58:23 24 4
gpt4 key购买 nike

My Login.php file


<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");

$mysql_host = "localhost";
$mysql_database = "test";
$mysql_user = "root";
$mysql_password = "";
// Create connection
$conn = new mysqli($mysql_host, $mysql_user, $mysql_password,$mysql_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$aemp=$_REQUEST['user'];
?>

我的login.html文件

<div class="list">
<label class="item item-input item-floating-label">
<span class="input-label">Username</span>
<input type="text" ng-model="user.userName" placeholder="Enter your username">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Password</span>
<input type="password" ng-model="user.password" placeholder="Enter yourPassword">
</label>
</div>
<button ng-click="login()" class="button button-outline button-stable" >

我的 mainserver.js 文件

app.service('mainService',function ($http,$q) {

this.login = function (userInfo) {
var defer = $q.defer();
$http.post('http://localhost/login.php',{user:userInfo})
.success(function (response) {
$scope.names = response.records;
if(res){
defer.resolve(response);
}else{
defer.reject(response);
}
})
.error(function (err) {
defer.reject(err);
})
return defer.promise
}

})

当我将对象发送到我的 php 文件时,它会向我显示
user obj 对象 {用户名:“asdas”,密码:“sadsaada”}
XMLHttpRequest 无法加载 http://localhost/login.php 。预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段 Content-Type。

最佳答案

js文件中

app.service('mainService',function ($http,$q) {
this.login = function (userInfo) {
var defer = $q.defer();
$http.post(
'http://localhost/login.php',
{user: userInfo},
{headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}}
)
.success(function (response) {
$scope.names = response.records;
if(res){
defer.resolve(response);
}else{
defer.reject(response);
}
})
.error(function (err) {
defer.reject(err);
})
return defer.promise
}

});

以及服务器端的php

header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Access-Control-Allow-Headers, accept");

关于php - ionic 2 与 php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37434776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com