Angular + Ionic Post请求未找到404 [英] Angular + Ionic Post request getting 404 not found
问题描述
我已经搜索了这个错误背后的原因,过去几个小时无法解决。希望有人遇到过同样的问题并能够帮助解决问题。
I have searched for the reason behind this error for the last couple hours unable to solve it. Hopefully someone has experienced the same issue and is able to help with a remedy.
我正在使用Ionic和Angular来创建一个简单的应用程序。我在本地使用Ionics ionic serve 命令来设置一个简单的服务器。
I am using Ionic and Angular to create a simple application. I am running locally using Ionics ionic serve command to set up a simple server.
我有一个简单的index.html页面,如下所示:
I have a simple index.html page that is as follows:
...
<body data-ng-controller="AppCtrl">
<ion-header-bar class="bar-balanced">
<h1 class="title ">Hello World!</h1>
</ion-header-bar>
<ion-content>
<h1>Some Content</h1>
</ion-content>
</body>
...
和一个已经多次迭代并试过的app.js文件最终的各种在线教程:
and an app.js file that has been iterated numerous times and tried various online tutorials to end up with:
var App = angular.module("App", ["ionic"]);
App.controller("AppCtrl", ["$scope", "$http" , AppCtrl]);
function AppCtrl($scope, $http) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
var request = $http({
method: 'post',
url: 'http://localhost:8100/php/login.php',
data: { "cat" : "henry" },
headers : {"Content-Type": 'application/x-www-form-urlencoded'}
}).success(function (data, status, headers, config) {
console.log(data);
}).error(function (data, status, headers, config) {
console.log("login.php failed");
});
};
我的问题:是我收到404 Not Found错误在我的帖子请求。但是,当我更改为get请求时,它似乎回复PHP文件就好了。它是一个简单的回声Hello World! php文件。
My problem: is that I am getting "404 Not Found" error on my post requests. However, when I change to a get request, it seems to echo back the PHP file just fine. Its a simple echo "Hello World!" php file.
此时,我们将非常感谢任何有用的信息。谢谢!
At this point, any helpful information would be greatly appreciated. Thank you!
推荐答案
我遇到了类似的问题,我这个网站帮助我理解了我的问题:
http:// victorblog。 com / 2012/12/20 / make-angularjs-http-service-behave-like-jquery-ajax /
I got a similar issue and I this site helped me to understand my problem:
http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/
默认情况下:
By default:
jQuery 使用内容类型传输数据:
x-www-form-urlencoded。
AngularJS 使用Content-Type传输数据:
application / json。
jQuery transmits data using Content-Type:
x-www-form-urlencoded.
AngularJS transmits data using Content-Type:
application/json.
不幸的是,有些Web服务器语言,特别是PHP,不要
本地反序列化。
Which unfortunately some Web server languages, notably PHP, do not unserialize natively.
我的解决方案是:
在我的.PHP中我刚刚在我的内容中做了一个JSON_DECODE
My solution was:
In my .PHP I just did a JSON_DECODE in my content
$ data = json_decode(file_get_contents('php:// input'),true);
$data = json_decode(file_get_contents('php://input'), true);
这篇关于Angular + Ionic Post请求未找到404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!