$http({
url: 'http://myURL.com/api/',
method: "POST",
data: { 'YTParam' : 'test' }
})
.then(function(response) {
console.log(response.data);
},
function(response) { // optional
// failed
}
);
In my console I see the entire html got printed out, I was expecting the value of $_POST[myParam]
. Why is that happening?
在我的控制台中,我看到整个html被打印出来,我期待$ _POST [myParam]的值。为什么会这样?
my PHP as below
我的PHP如下
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{
margin: 0;
}
</style>
</head>
<body>
<?php
echo $_POST["YTParam"];
if($_POST["YTParam"])
{
$YTParam = $_POST["YTParam"];
echo $YTParam;
}
?>
</body>
</html>
I did close every tag properly but have no idea why I have this problem.
我确实关闭了每个标签,但不知道为什么我有这个问题。
2 个解决方案
#1
4
Remove all the html on your .php file, as its role is not to be printed directly in a browser, but to treat data server-side.
删除.php文件中的所有html,因为它的角色不是直接在浏览器中打印,而是处理数据服务器端。
So, the remaining part would be :
所以,剩下的部分是:
<?php
echo $_POST["YTParam"]; // Will be returned to your JS
if($_POST["YTParam"])
{
$YTParam = $_POST["YTParam"];
echo $YTParam; // Will be returned to your JS
}
?>
Everything you'll echo
will be returned in response
, in your JS ajax call.
在JS ajax调用中,您将回复的所有内容都将作为响应返回。
Edit
Your data is not in the $_POST
array, because your angular JS contentType is set to application/json
by default, and PHP expects application/x-www-form-urlencoded
for a POST request. Quoting from docs :
您的数据不在$ _POST数组中,因为默认情况下您的angular JS contentType设置为application / json,而PHP期望application / x-www-form-urlencoded用于POST请求。引用文档:
$httpProvider.defaults.headers.post: (header defaults for POST requests)
$ httpProvider.defaults.headers.post :( POST请求的标头默认值)
Content-Type: application/json
Content-Type:application / json
To change this, either change this value in your config :
要更改此设置,请在配置中更改此值:
yourApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
}]);
Or at the time you send your request :
或者在您发送请求时:
$http({
url: 'http://myURL.com/api/',
method: "POST",
headers: {
'Content-Type': "application/x-www-form-urlencoded"
},
data: { 'YTParam' : 'test' }
})
You can set contentType
directly as a property, but I'm not 100% sure of this.
您可以将contentType直接设置为属性,但我不是100%肯定这一点。
#2
2
The PHP spits out the entire HTML and that's what gets sent to your browser. To correct that, you have to do your parsing before the HTML code and call exit(), so that no HTML gets sent to the browser. Take this code out of the HTML body:
PHP会吐出整个HTML,这就是发送到浏览器的内容。要纠正这个问题,您必须在HTML代码之前进行解析并调用exit(),这样就不会将HTML发送到浏览器。从HTML正文中获取此代码:
<?php
echo $_POST["YTParam"];
if($_POST["YTParam"])
{
$YTParam = $_POST["YTParam"];
echo $YTParam;
}
?>
And replace it with this. Placing it at the top of your script, before the opening tag:
并用它替换它。将它放在脚本的顶部,在开始标记之前:
<?php
if(isset($_POST["YTParam"]))
{
$YTParam = $_POST["YTParam"];
echo $YTParam;
exit;
}
?>
#1
4
Remove all the html on your .php file, as its role is not to be printed directly in a browser, but to treat data server-side.
删除.php文件中的所有html,因为它的角色不是直接在浏览器中打印,而是处理数据服务器端。
So, the remaining part would be :
所以,剩下的部分是:
<?php
echo $_POST["YTParam"]; // Will be returned to your JS
if($_POST["YTParam"])
{
$YTParam = $_POST["YTParam"];
echo $YTParam; // Will be returned to your JS
}
?>
Everything you'll echo
will be returned in response
, in your JS ajax call.
在JS ajax调用中,您将回复的所有内容都将作为响应返回。
Edit
Your data is not in the $_POST
array, because your angular JS contentType is set to application/json
by default, and PHP expects application/x-www-form-urlencoded
for a POST request. Quoting from docs :
您的数据不在$ _POST数组中,因为默认情况下您的angular JS contentType设置为application / json,而PHP期望application / x-www-form-urlencoded用于POST请求。引用文档:
$httpProvider.defaults.headers.post: (header defaults for POST requests)
$ httpProvider.defaults.headers.post :( POST请求的标头默认值)
Content-Type: application/json
Content-Type:application / json
To change this, either change this value in your config :
要更改此设置,请在配置中更改此值:
yourApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
}]);
Or at the time you send your request :
或者在您发送请求时:
$http({
url: 'http://myURL.com/api/',
method: "POST",
headers: {
'Content-Type': "application/x-www-form-urlencoded"
},
data: { 'YTParam' : 'test' }
})
You can set contentType
directly as a property, but I'm not 100% sure of this.
您可以将contentType直接设置为属性,但我不是100%肯定这一点。
#2
2
The PHP spits out the entire HTML and that's what gets sent to your browser. To correct that, you have to do your parsing before the HTML code and call exit(), so that no HTML gets sent to the browser. Take this code out of the HTML body:
PHP会吐出整个HTML,这就是发送到浏览器的内容。要纠正这个问题,您必须在HTML代码之前进行解析并调用exit(),这样就不会将HTML发送到浏览器。从HTML正文中获取此代码:
<?php
echo $_POST["YTParam"];
if($_POST["YTParam"])
{
$YTParam = $_POST["YTParam"];
echo $YTParam;
}
?>
And replace it with this. Placing it at the top of your script, before the opening tag:
并用它替换它。将它放在脚本的顶部,在开始标记之前:
<?php
if(isset($_POST["YTParam"]))
{
$YTParam = $_POST["YTParam"];
echo $YTParam;
exit;
}
?>