在AngularJS AJAX中调用php函数或php文件 [英] Calling php function or php file within AngularJS AJAX

查看:68
本文介绍了在AngularJS AJAX中调用php函数或php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 AngularJs ,使用 PHP / MySql 作为后端来获取/发送信息到数据库.

I've been working with AngularJs for a while, using PHP/MySql as backend to get/send information to the dataBase.

我的工作方式(及其工作方式)如下:

The way I do (and it's working) is like this:

var myFunction = 'getUser';
$http.get('my/php/file.php?action='+myFunction).then(function(response) {
    //get data here
})

//or

var myFunction = 'addUser';
$http.post('my/php/file.php?action='+myFunction, data).then(function(response) {
    //get data here
})

但是我也看到了很多项目(教程,文章等),他们这样称呼它(或类似的东西):

But I also saw a lot of projects (tutorials, articles, etc..) where they call it like this (or something like this):

$http.get('api/user/:id').then(function(response) {
    //get data here
})

//or
$http.post('api/user', data).then(function(response) {
    //get data here
})

如您所见,我的方式是声明要使用的功能, getUser addUser deleteUser 等...但是在我看到的示例中,他们没有这样做,只是调用了路径引用.

As you can see, the way I do, I declare what function I want to use, getUser, addUser, deleteUser, etc... But on the examples I saw, they don't do it, they just call a path reference.

一种方法或另一种方法的主要区别是什么?什么时候(或为什么)应该使用一个或另一个?

What is the main difference between one method or the other? When (or why) should I use one or another?

推荐答案

这完全取决于您所开发的服务

It all depends on the services that your developing

var myFunction = 'getUser';
$http.get('my/php/file.php?action='+myFunction).then(function(response) {
    //get data here
})

//or

var myFunction = 'addUser';
$http.post('my/php/file.php?action='+myFunction, data).then(function(response) {
    //get data here
})

此对应于具有端点的服务,并通过端点处理请求和响应

This one corresponds to the services which has endpoints and the request and response is handled through them

也是这个

$http.get('api/user/:id').then(function(response) {
    //get data here
})

//or
$http.post('api/user', data).then(function(response) {
    //get data here
})

与RESTful服务相对应,其中HTTP动词与相应的资源匹配,并且服务得到执行

corresponds to the RESTful services where the http verbs match to the corresponding resources and the services gets executed

这篇关于在AngularJS AJAX中调用php函数或php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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