php - 同时写了两个http服务导致angular崩溃
天蓬老师
天蓬老师 2017-04-11 10:36:35
0
2
509
myapp1 = angular.module('myApp', []);


myapp1.controller('jgCtrl', function($scope,$http) {

$http({
    method:'post',
    url:'json.php',
    data:{name:'test',age:20},
}).then(function successCallback(response) {
    alert('添加成功');
}, function errorCallback(response) {
    alert('添加失败');
});

$http.get('json.php',{
}).then(function(res){
    $scope.items=res.data;
    console.log(res);
    },function(){
      alert('fault');
      });
});

代码如上,我的目的是向php发送一个请求,使它可以根据我发出的data返回我想要的数据。
单下面的httpget运行是没有问题,可以正常接收表单的,但是我该如何修改代码才能使得前端能向php发送字段呢

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

Antworte allen(2)
刘奇

谢邀,但我没明白你的问题。

我想,你是想说第一个 $http 发送失败吗?代码上并无问题,我想应该是php后端无法正常获取数据吧,这是因为 $http 默认内容类型是 content-type: javascript/json,所以你只要确认PHP是否支持即可。

当然,你也可以在你前端换成表单内容类型,即:

$http({
    method:'post',
    url:'json.php',
    data:{name:'test',age:20},
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
}).then(function successCallback(response) {
    alert('添加成功');
}, function errorCallback(response) {
    alert('添加失败');
});
大家讲道理

你能把控制台的错误提示贴出来吗,我觉得并不一定是你的两次请求导致的问题

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!