为了账号安全,请及时绑定邮箱和手机立即绑定

AngularJS POST失败:飞行前响应具有无效的HTTP状态代码404

AngularJS POST失败:飞行前响应具有无效的HTTP状态代码404

哔哔one 2019-10-19 14:14:52
我知道有很多这样的问题,但是我所见的问题都没有解决。我已经使用了至少3个微框架。所有这些都无法执行简单的POST,它应该返回数据:angularJS客户端:var app = angular.module('client', []);app.config(function ($httpProvider) {  //uncommenting the following line makes GET requests fail as well  //$httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = '*';  delete $httpProvider.defaults.headers.common['X-Requested-With'];});app.controller('MainCtrl', function($scope, $http) {  var baseUrl = 'http://localhost:8080/server.php'  $scope.response = 'Response goes here';  $scope.sendRequest = function() {    $http({      method: 'GET',      url: baseUrl + '/get'    }).then(function successCallback(response) {      $scope.response = response.data.response;    }, function errorCallback(response) { });  };  $scope.sendPost = function() {    $http.post(baseUrl + '/post', {post: 'data from client', withCredentials: true })    .success(function(data, status, headers, config) {      console.log(status);    })    .error(function(data, status, headers, config) {      console.log('FAILED');    });  }});SlimPHP服务器:<?php    require 'vendor/autoload.php';    $app = new \Slim\Slim();    $app->response()->headers->set('Access-Control-Allow-Headers', 'Content-Type');    $app->response()->headers->set('Content-Type', 'application/json');    $app->response()->headers->set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');    $app->response()->headers->set('Access-Control-Allow-Origin', '*');    $array = ["response" => "Hello World!"];    $app->get('/get', function() use($array) {        $app = \Slim\Slim::getInstance();        $app->response->setStatus(200);        echo json_encode($array);    }); 我已启用CORS,并且GET请求有效。html使用服务器发送的JSON内容进行更新。但是我得到了XMLHttpRequest无法加载http:// localhost:8080 / server.php / post。飞行前的响应具有无效的HTTP状态代码404每次我尝试使用POST时。为什么?
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 865 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信