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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分