$httppost in angularJS transmits data to php to receive problems angularjs 2.0 angularjs video tutorial angularjs directive

WBOY
Release: 2016-07-29 08:53:58
Original
910 people have browsed it

I recently learned angularJS and found the problem that using its own $http.post to send data to the background php, php cannot receive it. For example:

$http.post("php/getRoleRight.php", {rr:$scope.currSelect}).success(function(response) { ·· });
Copy after login
PHP cannot receive the passed parameters when using
$_POST['rr']
Copy after login
.
angular,angularjs 教程,angularjs下载,angularjs官网,angularjs中文社区,angularjs api,angularjs 2.0,angularjs 视频教程,angularjs directive
I opened firebug and found that the transfer was successful. After struggling for a long time, I found out that the format of the transfer is different from the ordinary transfer format, such as the $.post() method in jQuery, $http.post() It is transmitted in json format, while $.post() is transmitted in the form of form parameters, and PHP's $.POST[] expects to receive the data transmitted from the front desk in the latter form, so the data transmitted by json cannot be read. .

angular,angularjs 教程,angularjs下载,angularjs官网,angularjs中文社区,angularjs api,angularjs 2.0,angularjs 视频教程,angularjs directive

After looking online and doing my own experiments, there are two ways to solve this problem:

1. Simply and crudely change it directly to the $.post() method, so that no modification is used in the background, but some online More experienced netizens said that this may defeat the original intention of using angular.

2. Modify the php code

$postData = file_get_contents('php://input', true);
$obj=json_decode($postData);	
$query1 = "SELECT * FROM role_roleRight  WHERE roleId=$obj->rr";	
Copy after login
first reads the json string and then parses it into an object. In this way, the value of the passed parameter rr can be obtained using the object attribute.

Okay, you’re done.

The above introduces the problem of $httppost in angularJS transmitting data to php for receiving, including the content of angularjs. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template