gpt4 book ai didi

PHP 没有响应 Angularjs 请求

转载 作者:可可西里 更新时间:2023-11-01 13:20:45 25 4
gpt4 key购买 nike

我有一个带有一些 angularjs 验证的原始表单,

<form name = "sform" novalidate="true">
<table border="0">
<tr>
<td>
First name:
</td>
<td>
<input type="text" name="fname" ng-model="fname" placeholder="First Name" required="true" ng-pattern="/^[A-Za-z]*$/">
</td>
<td ng-show="sform.fname.$invalid && !sform.fname.$pristine" class="help-block">wrong name </td>
</tr>

<tr>
<td>
Last Name:
</td>
<td>
<input type="text" ng-model="lname" placeholder="last Name" required="true" ng-pattern="/^[A-Za-z]*$/">
</td>
<td ng-show= "sform.lname.$invalid && !sform.lname.$pristine" class="help-block">wrong Last name </td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="email" ng-model="email" placeholder="email" required="true">
</td>
<td ng-show="sform.email.$invalid && !sform.email.$pristine" class="help-block">wrong Email </td>
</tr>
<tr>
<td>
<button type="submit" ng-disabled="sform.$invalid" ng-click = "submitForm()">Submit</button>
</td>
</tr>
</table>
</form>

相关的.js文件是

far_app.js

var farLogin = angular.module('far_login',[]);

far_formcontroler.js

farLogin.controller('far_formcontrol',['$scope','$http',function($scope,$http) {
$scope.url='http://localhost/far_submit.php';
var config = {
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
}

// function to submit the form after all validation has occurred
$scope.submitForm = function() {
alert('our form is amazing'+$scope.fname+' '+$scope.email+' '+$scope.lname);
// check to make sure the form is completely valid
if ($scope.sform.$valid) {
$http.post($scope.url,{"name": $scope.fname, "email": $scope.email, "lname": $scope.lname},config).
success(function() {
alert('our form is amazing'+$scope.fname+' '+$scope.email+' '+$scope.lname);
})

}
else
{
alert('our form is not amazing');
}

}

}]);

我的php文件

<?php
header("Access-Control-Allow-Origin: *");
?>
<script type="text/javascript"> alert("this is php")</script>
<?php
?>

但是 php 脚本根本没有执行,它们在浏览器的控制台中也没有错误。

我哪里出错了?

谢谢。

最佳答案

在 SO 本身的 Angular $http post issues 上有几个很好的线程,例如看看下面的线程:How do I POST urlencoded form data with $http in AngularJS?或者这个:How can I post data as form data instead of a request payload?

注意:您需要使用本地网络服务器运行它。下面的代码对我有用,我做了以下操作:

  • 下载了您提供的代码
  • 将它们全部保存在本地目录
  • 启动内置于网络服务器的 php php -S 0.0.0.0:8181,访问位于:http://localhost:8181/ 的 index.html提交了表单,它返回了一个 json 响应:{"status":"OK","data":{"userid":null}}

Pastebin:far_app.js , index.html , far_login.php

我使用 PHP 内置服务器只是为了测试目的,但是,我强烈建议您在 Windows 上使用 WamppXampp 来运行它。您也可以手动配置 Apache/PHP/Mysql,但是 Wampp/Xampp 非常易于安装和使用。

我对代码进行了一些重组,如下所示:

    angular.module('far_login',[])
.controller('far_formcontrol',['$scope','$http', function($scope, $http) {
// function to submit the form after all validation has occurred
$scope.submitForm = function() {
console.log('First Name : ' , $scope.fname);
console.log('Email : ' , $scope.email);
console.log('Last Name: ', $scope.lname);
// check to make sure the form is completely valid
if ($scope.sform.$valid) {
$http({
url: 'http://localhost:8181/far_submit.php',
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: {"name": $scope.fname, "email": $scope.email, "lname": $scope.lname}
})
.then(function(response) {
console.log(response)
})
.catch(function (error) {
console.log(error);
});

} else {
alert('our form is not amazing');
}
};
}]);

这是基于 Plunker来自 Angular docfar_submit.php 文件,如:

<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');

$data = [ 'status' => 'OK', 'data' => [ 'userid' => $autogeneratedUserId] ];
echo json_encode($data);

确保 far_submit.php 可以在 http://localhost/far_submit.php 访问

关于PHP 没有响应 Angularjs 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36686807/

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