我正在构建一个应用程序,并使用下面的测试代码来测试角度http.Post()
但是我收到一个奇怪的错误,我的预检没有成功。
客户端代码位于
之下<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
{{ names }}
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.post("http://ec2-52-3-54-45.compute-1.amazonaws.com/api/user/dogs",{id:'hello word!'})
.success(function (response) {
$scope.names = response});
});
</script>
</body>
</html>
另一端的.htaccess文件位于
之下 Header set Access-Control-Allow-Methods "POST"
Header set Access-Control-Allow-Origin "*"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
应该处理这个问题的PHP是
<?php
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
header( ‘Access-Control-Allow-Headers: Authorization, Content-Type’ );
header("Access-Control-Allow-Origin: *");
我不确定为什么我会收到此错误或我究竟做错了什么。非常感谢任何帮助或见解。
答案 0 :(得分:3)
这个htaccess代码应该可以工作:
#include <vector>
#include <thread>
#include <stdexcept>
using namespace std; // for expose only (don't do this in real code!!)
template <typename T> class Matrix
{
using row = vector<T>;
size_t _rows, _cols;
vector<row> _matrix;
Matrix(vector<row>&&); // defined elsewhere
row sumLine(row const& first, row const& second) const
{
row result; // don't tempt the compiler to default
result.reserve(_cols); // initialise result[i] for all columns
for(size_t c=0; c!=_cols; ++c)
result.emplace_back(first[c]+second[c]);
return result; // return is fast (no copy!)
}
Matrix<T> operator+ (const Matrix<T>& other) const;
};
template <typename T>
Matrix<T> Matrix<T>::operator+(const Matrix<T>&other) const
{
if(other._cols != _cols)
throw std::runtime_error("column number mismatch in Matrix+Matrix");
if(other._rows != _rows)
throw std::runtime_error("row number mismatch in Matrix+Matrix");
vector<thread> threads; threads.reserve(_rows);
vector<row> result(_rows);
for(size_t r=0; r!=_rows; ++r)
threads.emplace_back([&,r]() {
result[r] = Matrix<T>::sumLine(_matrix[r],other._matrix[r]);
});
for(size_t r=0; r!=_rows; ++r)
threads[r].join();
return move(result);
}
template class Matrix<double>;
Angularjs在发出POST请求之前发出OPTIONS请求