Javascript 角度+;php不会从not index.html向服务器加载数据
我有这样的问题:我的应用程序由index.html和一堆其他html文件组成,这些文件用于我的应用程序的不同页面,我通过ng路线导航。我想把一些数据发布到服务器上,但我只能在index.html上发布 这是我的角度代码:Javascript 角度+;php不会从not index.html向服务器加载数据,javascript,php,html,angularjs,Javascript,Php,Html,Angularjs,我有这样的问题:我的应用程序由index.html和一堆其他html文件组成,这些文件用于我的应用程序的不同页面,我通过ng路线导航。我想把一些数据发布到服务器上,但我只能在index.html上发布 这是我的角度代码: //post data to DB app.controller('sign_up', function ($scope, $http) { $scope.check_credentials = function () { document.
//post data to DB
app.controller('sign_up', function ($scope, $http) {
$scope.check_credentials = function () {
document.getElementById("message").textContent = "";
var request = $http({
method: "post",
url: window.location.href + "php/add_review.php",
headers: { 'Content-Type': 'application/json' },
data: {
review: $scope.review
}
});
request.success(function (data) {
document.getElementById("message").textContent = "You have login successfully with email "+data+request;
});
}
});
我的PHP代码:
<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$review = $request->review;
mysql_connect("localhost", "root", "");
mysql_select_db("reherse");
mysql_query("INSERT INTO reviews(review) VALUES('".$review."')");
//echo $request
echo $review;
?>
尝试使用window.location.origin
而不是window.location.href
。前者只提供协议、主机名和端口(例如http://localhost:8080
)而后者将为您提供整个URL(例如http://localhost:8080/somepage.html
)
这就是HTTP请求在非索引页面上失败的原因,因为它试图加载的URL不正确;它是http://localhost:8080/somepage.html/php/add_review.php
而不是http://localhost:8080/php/add_review.php
。它很可能对索引起作用,因为您没有指定index.html
,而只是加载http://localhost:8080
,在这种情况下,window.location.origin
将等于window.location.href
(某种程度上,请参见下面的注释),并为您提供正确的URL
请注意,window.location.origin
不包含尾随斜杠,因此请确保HTTP请求的URL字符串如下所示:
window.location.origin+“/php/add\u review.php”
<div id="login" ng-controller='sign_up'>
<input type="text" size="40" ng-model="review"><br>
<input type="password" size="40" ng-model="review"><br>
<button ng-click="check_credentials()">Login</button><br>
<span id="message"></span>
</div>