donglao6169 2016-10-07 02:38
浏览 55
已采纳

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

well i have this problem about sending multi data from JS to PHP, i'm using angularJS. the problem is i can't receive any data, i'm a beginner in this stuff like AngularJS.this my my JS part:

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 :

<?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.{}. the problem is this $_POST['name'] doesn't work; 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:

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
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.

  • 写回答

1条回答 默认 最新

  • doujiaochan7317 2016-10-07 02:49
    关注

    Arguments and config are wrong for $http.post( url, data, config)

    Remove params from config, those are for GET url query string params

    EDIT: if all you are setting is the 'Content-Type header you don't need a config....application/json is the default

    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')
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目