Ajax请求的角度JS的PHP [英] Ajax request to php from angular js

查看:87
本文介绍了Ajax请求的角度JS的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个Ajax请求的角度JS到PHP。但我没有得到我所发送的数据。请让我知道我错了。

I am trying make an ajax request to php from angular js. But I am not getting the data I have sent. Please let me know where I am going wrong.

以下是AJAX调用。

<script type="text/javascript">

    angular.module('app', []).controller("MyController", function($scope, $http) {

    var req = {
        method: 'POST',
        url: 'pujastrail/www/rest/get.php',
        headers: {
            'Content-Type': "application/x-www-form-urlencoded; charset=utf-8"
        },
        data: { lang: "fr" }
    }
    $http(req).success(function(data, status, headers, config){alert("done"+data);}).error(function(data, status, headers, config){alert("error"+res);});

        });
</script>

下面是PHP code。

The following is the php code.

<?php
    if (isset($_POST['lang']) && !empty($_POST['lang'])) {
        $lang = $_POST['lang'];//this needs to be sent back to js file
    } else {
        $lang = "eRROR";// but this is being sent back to the js file
    }

    echo (json_encode($lang));
?>

我已经试过这一点使用$ _REQUEST也却因此未工作。

I have tried this using $_REQUEST also but it didnot work.

推荐答案

在你的角度code,你需要连接code中的参数数据,如字符串LANG = FR 而不是 {郎咸平:FR}

In your angular code, you need to encode the parameter data as a string like lang=fr instead of {lang: 'fr'}.

尝试使用 $参数(OBJ) $参数({郎咸平:FR})。将其转换是这样的:

Try using $.param(obj) or $.param({lang: "fr"}) to convert it like this:

var req = {
    method: 'POST',
    url: 'pujastrail/www/rest/get.php',
    headers: {
        'Content-Type': "application/x-www-form-urlencoded; charset=utf-8"
    },
    data: $.param({ lang: "fr" })
}

source

这篇关于Ajax请求的角度JS的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆