I am using Angular to send some json data to server. It gives me the entire php file as data in return. On my bootstap modal I have ng-submit=createNewProgram()
我正在使用Angular将一些json数据发送到服务器。它给了我整个php文件作为数据作为回报。在我的bootstap模式我有ng-submit = createNewProgram()
$scope.createNewProgram = function()
{
var data = {
'programName': $scope.programName,
'startDate' : $scope.startDate,
'endDate' : $scope.endDate,
'startTime': $scope.startTime,
'endTime' :$scope.endTime
};`
$http({
method : 'POST',
url : 'curl.php',
data : data, //forms user object
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
if (data.errors) {
// Showing errors.
$scope.message = 'wrong data';
} else {
$scope.message = data;
}
});
}
This is how I am recieving data through my php file (curl.php):
这是我通过我的php文件(curl.php)收到数据的方式:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$programName = $request->programName;
@$startDate = $request->startDate;
echo $programName;
3 个解决方案
#1
2
Actually your writing code has some problem.You include a " ` " before $http part.
实际上你的编写代码有一些问题。你在$ http部分之前加入了一个“`”。
var data = {
'programName': $scope.programName,
'startDate' : $scope.startDate,
'endDate' : $scope.endDate,
'startTime': $scope.startTime,
'endTime' :$scope.endTime
};`
You do not need to write this code in curl.php file .You can get value of programName parameter using $_REQUEST super global variable.
您不需要在curl.php文件中编写此代码。您可以使用$ _REQUEST超级全局变量获取programName参数的值。
$scope.createNewProgram = function()
{
var data = {
'programName': $scope.programName,
'startDate' : $scope.startDate,
'endDate' : $scope.endDate,
'startTime': $scope.startTime,
'endTime' :$scope.endTime
};
$http({
method: "POST",
url: "curl.php",
params: data,
headers:{
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) { // Success time
$scope.message = response.data;
}, function(response) { // Faulure time
$scope.message = 'wrong data';
});
}
The above mentioned is the correct angular js code for your mentioned code.
上面提到的是你提到的代码的正确角度js代码。
If you want to get "programName" parameter value,write php code like that
如果你想获得“programName”参数值,那就写那样的php代码
<?php
if( isset($_REQUEST["programName"]) === true ) {
echo $_REQUEST["programName"];
} else {
echo "";
}
?>
#2
0
You are sending POST data as content-type.
您将POST数据作为内容类型发送。
Use application/json
to get json from http call.
使用application / json从http调用中获取json。
使用PHP阅读JSON POST
#3
0
Edit - JS Code:
编辑 - JS代码:
$scope.createNewProgram = function()
{
var data = {
'programName': $scope.programName,
'startDate' : $scope.startDate,
'endDate' : $scope.endDate,
'startTime': $scope.startTime,
'endTime' :$scope.endTime
};
$http({
method : 'POST',
url : 'curl.php',
data : $.param(data), /*forms user object*/
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
$scope.message = data;
})
.error(function(error) {
$scope.message = 'error';
});
}
** Note the $.param function is a jQuery function, so you will have to include jQuery in your html file.
**注意$ .param函数是一个jQuery函数,所以你必须在你的html文件中包含jQuery。
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
if (isset($_POST['programName'])) {
echo $_POST['programName'];
} else {
echo 'Error Occured.'
}
?>
#1
2
Actually your writing code has some problem.You include a " ` " before $http part.
实际上你的编写代码有一些问题。你在$ http部分之前加入了一个“`”。
var data = {
'programName': $scope.programName,
'startDate' : $scope.startDate,
'endDate' : $scope.endDate,
'startTime': $scope.startTime,
'endTime' :$scope.endTime
};`
You do not need to write this code in curl.php file .You can get value of programName parameter using $_REQUEST super global variable.
您不需要在curl.php文件中编写此代码。您可以使用$ _REQUEST超级全局变量获取programName参数的值。
$scope.createNewProgram = function()
{
var data = {
'programName': $scope.programName,
'startDate' : $scope.startDate,
'endDate' : $scope.endDate,
'startTime': $scope.startTime,
'endTime' :$scope.endTime
};
$http({
method: "POST",
url: "curl.php",
params: data,
headers:{
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) { // Success time
$scope.message = response.data;
}, function(response) { // Faulure time
$scope.message = 'wrong data';
});
}
The above mentioned is the correct angular js code for your mentioned code.
上面提到的是你提到的代码的正确角度js代码。
If you want to get "programName" parameter value,write php code like that
如果你想获得“programName”参数值,那就写那样的php代码
<?php
if( isset($_REQUEST["programName"]) === true ) {
echo $_REQUEST["programName"];
} else {
echo "";
}
?>
#2
0
You are sending POST data as content-type.
您将POST数据作为内容类型发送。
Use application/json
to get json from http call.
使用application / json从http调用中获取json。
使用PHP阅读JSON POST
#3
0
Edit - JS Code:
编辑 - JS代码:
$scope.createNewProgram = function()
{
var data = {
'programName': $scope.programName,
'startDate' : $scope.startDate,
'endDate' : $scope.endDate,
'startTime': $scope.startTime,
'endTime' :$scope.endTime
};
$http({
method : 'POST',
url : 'curl.php',
data : $.param(data), /*forms user object*/
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
$scope.message = data;
})
.error(function(error) {
$scope.message = 'error';
});
}
** Note the $.param function is a jQuery function, so you will have to include jQuery in your html file.
**注意$ .param函数是一个jQuery函数,所以你必须在你的html文件中包含jQuery。
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
if (isset($_POST['programName'])) {
echo $_POST['programName'];
} else {
echo 'Error Occured.'
}
?>