我知道有很多這樣的問題,但是我所見的問題都沒有解決。我已經(jīng)使用了至少3個(gè)微框架。所有這些都無法執(zhí)行簡(jiǎn)單的POST,它應(yīng)該返回?cái)?shù)據(jù):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服務(wù)器:<?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請(qǐng)求有效。html使用服務(wù)器發(fā)送的JSON內(nèi)容進(jìn)行更新。但是我得到了XMLHttpRequest無法加載http:// localhost:8080 / server.php / post。飛行前的響應(yīng)具有無效的HTTP狀態(tài)代碼404每次我嘗試使用POST時(shí)。為什么?
AngularJS POST失?。猴w行前響應(yīng)具有無效的HTTP狀態(tài)代碼404
嗶嗶one
2019-10-19 14:14:52