简体   繁体   English

如何使用Json将angularJS控制器中的数据发送到PHP

[英]how to send data from angularJS controler to PHP using Json

well i have this problem about sending multi data from JS to PHP, i'm using angularJS. 好吧,我有关于从JS向PHP发送多数据的问题,我正在使用angularJS。 the problem is i can't receive any data, i'm a beginner in this stuff like AngularJS.this my my JS part: 问题是我无法接收任何数据,我是AngularJS之类的东西的初学者。这是我的JS部分:

var data = {"name":name, "cin":cin, "job":job};

var config = {
        params: data,
        headers: { 'Content-Type': 'application/json' }
    };

    $request = "php/readFromDatabase.php";
    $http.post($request, config).then(function (response) {
        $scope.collectionData = response.data.records;
    });

this should be fine and work i have my data as struct (object) the name and cin and job are variables when i click button using angularJS controller this function should be launched and call an PHP file : 这应该可以正常工作,我将数据作为struct(对象)时,名称和cin和job是变量,当我使用angularJS控制器单击按钮时,应该启动此函数并调用PHP文件:

<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
$name = $_POST['name'];

i use those information to look for data in database and then i echo them as JSON format for that i want to use $http.get{} so i can call the response.data.records and not to use the $ajax.{}. 我使用这些信息在数据库中查找数据,然后将它们作为JSON格式进行回显,因此我想使用$ http.get {},以便可以调用response.data.records而不使用$ ajax。{} 。 the problem is this $_POST['name'] doesn't work; 问题是此$ _POST ['name']不起作用; even this didn't work: 即使这样也不起作用:

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$name = $request->name;

As i said, i'm new here so plz tell me what i'm doing wrong because i didn't find anything that can help me, i tied everything i have found in internet. 就像我说的,我是新来的,所以请告诉我我做错了什么,因为我没有找到任何可以帮助我的东西,我把在互联网上找到的所有东西都绑了起来。 hope if someone can help me and thank you in advance. 希望有人能帮助我,并提前感谢您。

Edit :

this is my JS file: 这是我的JS文件:

app.controller('shearch_data', function ($scope, $http) {
$scope.ShearchFunction = function () {

    var name = " ";
    if ($('#toggle_name').prop('checked') == true)
        name = $('#inputName').val();

    var cin = " ";
    if ($('#toggle_cin').prop('checked') == true)
        cin = $('#inputCIN').val();

    var job = " ";
    if ($('#toggle_job').prop('checked') == true)
        job = $('#inputJob').val();

    var data = {'name': "ffss", 'cin': cin,  'job': job};

    var url = "php/readFromDatabase.php";
    $http.post(url, data).then(function (response) {
        $scope.collectionData = response.data.records;
    }).catch(function (err) {
        alert('something went wrong')
    });

};
});

my PHP : 我的PHP:

<?php
header("Access-Control-Allow-Origin: *");
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$name = $request->name;
$cin = $request->cin;
$job = $request->job;
$output = "";
$output .='{"Name":"'.$name.'",';
$output .='"Job":"'.$job.'",';
$output .='"Cin":"'.$cin.'"}';
$output = '{"records":[' . $output . ']}';
echo($output);

this is what i'm trying to do for test, send data from JS to PHP using http.post then i receive it as json, it's jsut for test for now. 这就是我要进行的测试,使用http.post将数据从JS发送到PHP,然后将其作为json接收,目前是用于测试的jsut。

Arguments and config are wrong for $http.post( url, data, config) $http.post( url, data, config)参数和配置错误

Remove params from config, those are for GET url query string params 从配置中删除params ,这些params用于GET url查询字符串参数

EDIT: if all you are setting is the 'Content-Type header you don't need a config.... application/json is the default 编辑:如果您要设置的只是'Content-Type标头,则不需要配置。... application/json是默认设置

var config = {       
    headers: { 'Content-Type': 'application/json' }
};

var url = "php/readFromDatabase.php";
$http.post(url, data, config).then(function (response) {
    $scope.collectionData = response.data.records;
}).catch(function(err){
    alert('Ooops...something went wrong')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM