如何通过Ajax API处理多个请求?

时间:2022-11-29 13:58:16

I have a large project that requires multiple functions to be processed through Ajax calls, so far I am using different files to handle each function call. I know there's got to be a better way to handle the requests through an API

我有一个大项目需要通过Ajax调用处理多个函数,到目前为止我使用不同的文件来处理每个函数调用。我知道必须有更好的方法来通过API处理请求

And what would be a standard practice for designing such an API in terms of structure?

那么在结构方面设计这样一个API的标准做法是什么?

Thanks!

谢谢!

2 个解决方案

#1


5  

It can be everything from something quite simple, to something more complicated, how you write it is up to you, assuming you're the only one who's going to use the API, and as far as I know there are no standards set in stone for this kind of thing, but a good way to solve things like this is to create a convenience function, both clientside and serverside, say something like

它可以是从简单的东西到更复杂的东西,你如何编写它取决于你,假设你是唯一一个将使用API​​的人,据我所知,没有标准设置在石头上对于这种事情,但解决这类问题的一个好方法是创建一个方便的功能,无论是客户端还是服务器端,比如像

function fetch(what, data) {
    data.what = what;

    return $.ajax({
        url      : '/my/ajaxapi.php',
        data     : data,
        dataType : 'json'
    });
}

And when you need something you do

当你需要做某事时

fetch('users', {name : 'Bill'}).done(function(result) {
    $('#user').text(result.text);
});

$('.zoidberg').on('click', function() {
    var self = this;

    fetch('zoidberg', {}).done(function(result) {
        $(self).text(result.text);
    });
});

on the serverside you have one file, and how you set it up depends on the language used, but say PHP, and you could of course get fancy with classes etc. or just do a simple if/else or switch/case

在服务器端你有一个文件,你如何设置它取决于使用的语言,但说PHP,你当然可以看到类等等或只是做一个简单的if / else或开关/案例

<?php

    $key = $_GET['what']; // wrap in something that makes it safe
    $res = Array();

    switch($key) {
        case 'users' : 
           // lookup in DB or something
           $res['text'] = $username;
        break;
        case 'zoidberg' :
           $res['text'] = " Why Must I Be a Crustacean in Love";
    }

    echo json_encode($res);

?>

It's just a quick example, but set it up so it fits what you're doing, and it can save you a lot of work, and you have everything in one place so it's easy to find and modify later, easy to add new things that can be gotten with ajax etc. and possibly only need to get resources, DB connection etc. once, and not in every file.

这只是一个快速的例子,但是设置它以适应你正在做的事情,它可以为你节省大量的工作,你可以在一个地方拥有所有东西,这样以后很容易找到和修改,很容易添加新的东西可以用ajax等获得,可能只需要获取资源,数据库连接等一次,而不是每个文件。

#2


1  

Here is what I think you can do -

这是我认为你可以做的 -

Instead of creating multiple files to process AJAX request, just make a single file and then create different functions for different types of operations.

而不是创建多个文件来处理AJAX请求,只需创建一个文件,然后为不同类型的操作创建不同的函数。

Specify in your AJAX request to which function you want to call and then run only that function.

在AJAX请求中指定要调用的函数,然后仅运行该函数。

By this, way you don't have to manage multiple files for different types of operations.

这样,您就不必为不同类型的操作管理多个文件。

e.g.

例如

jQuery:

jQuery的:

$.ajax({
        type     : "POST",
        url      : 'myfile.php',
        data     : {'action': 'func1_name', 'val': 'some_value'} //wants to call a specific function 
    },
success:function(response){
console.log(response);
}
);

PHP:

PHP:

//executing func1
if(isset($_POST['action']) && $_POST['action'] == 'func1_name'){
    func1();
}

function func1(){
   if(isset($_POST['val']))
   echo $_POST['val'];
   die();
}

function func2(){

}

#1


5  

It can be everything from something quite simple, to something more complicated, how you write it is up to you, assuming you're the only one who's going to use the API, and as far as I know there are no standards set in stone for this kind of thing, but a good way to solve things like this is to create a convenience function, both clientside and serverside, say something like

它可以是从简单的东西到更复杂的东西,你如何编写它取决于你,假设你是唯一一个将使用API​​的人,据我所知,没有标准设置在石头上对于这种事情,但解决这类问题的一个好方法是创建一个方便的功能,无论是客户端还是服务器端,比如像

function fetch(what, data) {
    data.what = what;

    return $.ajax({
        url      : '/my/ajaxapi.php',
        data     : data,
        dataType : 'json'
    });
}

And when you need something you do

当你需要做某事时

fetch('users', {name : 'Bill'}).done(function(result) {
    $('#user').text(result.text);
});

$('.zoidberg').on('click', function() {
    var self = this;

    fetch('zoidberg', {}).done(function(result) {
        $(self).text(result.text);
    });
});

on the serverside you have one file, and how you set it up depends on the language used, but say PHP, and you could of course get fancy with classes etc. or just do a simple if/else or switch/case

在服务器端你有一个文件,你如何设置它取决于使用的语言,但说PHP,你当然可以看到类等等或只是做一个简单的if / else或开关/案例

<?php

    $key = $_GET['what']; // wrap in something that makes it safe
    $res = Array();

    switch($key) {
        case 'users' : 
           // lookup in DB or something
           $res['text'] = $username;
        break;
        case 'zoidberg' :
           $res['text'] = " Why Must I Be a Crustacean in Love";
    }

    echo json_encode($res);

?>

It's just a quick example, but set it up so it fits what you're doing, and it can save you a lot of work, and you have everything in one place so it's easy to find and modify later, easy to add new things that can be gotten with ajax etc. and possibly only need to get resources, DB connection etc. once, and not in every file.

这只是一个快速的例子,但是设置它以适应你正在做的事情,它可以为你节省大量的工作,你可以在一个地方拥有所有东西,这样以后很容易找到和修改,很容易添加新的东西可以用ajax等获得,可能只需要获取资源,数据库连接等一次,而不是每个文件。

#2


1  

Here is what I think you can do -

这是我认为你可以做的 -

Instead of creating multiple files to process AJAX request, just make a single file and then create different functions for different types of operations.

而不是创建多个文件来处理AJAX请求,只需创建一个文件,然后为不同类型的操作创建不同的函数。

Specify in your AJAX request to which function you want to call and then run only that function.

在AJAX请求中指定要调用的函数,然后仅运行该函数。

By this, way you don't have to manage multiple files for different types of operations.

这样,您就不必为不同类型的操作管理多个文件。

e.g.

例如

jQuery:

jQuery的:

$.ajax({
        type     : "POST",
        url      : 'myfile.php',
        data     : {'action': 'func1_name', 'val': 'some_value'} //wants to call a specific function 
    },
success:function(response){
console.log(response);
}
);

PHP:

PHP:

//executing func1
if(isset($_POST['action']) && $_POST['action'] == 'func1_name'){
    func1();
}

function func1(){
   if(isset($_POST['val']))
   echo $_POST['val'];
   die();
}

function func2(){

}