AJAX POST request with GET Parameters in URL

David Yue

Can you hard code GET Variables in a AJAX POST Request?

For Example:

Javascript:

$.ajax({
    url:"/script.php?type=1"
    type: "POST"
    data:{...}
    success: function()

})

PHP:
$a = $_GET["type"]
$b = $_POST["some data"]

Why do I need this?
I would like to have one script that would run multiple tiny functions depending on the one requested. (Since I don't want my server to be clogged with files that serve only one tiny purpose)I decided to use the URL GET Variable to decide that option because I do not want to mix the information that is related to the script(the POST data) to the actual "mode" the script should run in.

If this doesn't work, would there be any similar alternative that would not involve creating loads of files?

adeneo

Why not just use a POST variable when doing a POST request?
Quick lesson in making things easier ...

function ajax(type, data) {
    return $.ajax({
        url:"/script.php",
        type: "POST",
        data: $.extend(data, {type : type})
    });
}

ajax('db', {key : "value"}).done(function(data) { 
    if ( data === 'success' ) alert('insert happened!');
});
ajax('users', {user : "Bob"}).done(function(userData) {
    alert(userData);
});

and in PHP

<?php

    switch( $_POST['type'] ) {
        case "db" : 
            mysqli_query($sb, 'INSERT ' . $_POST['key'] . 'INTO SOMETHING');
            echo "success";
        break;
        case "user" : 
           echo get_user_data($_POST['user']);
        break;
        default : echo "These aren't the droids you're looking for";
    }

?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Is it possible to get POST parameters from REST request?

来自分类Dev

在AJAX请求后,URL会自动附加$ _GET-parameters

来自分类Dev

URL中带有GET参数的AJAX POST请求

来自分类Dev

PHP $ _POST,$ _ GET或$ _REQUEST

来自分类Dev

Ajax发送get而不是post

来自分类Dev

AJAX 发送 GET 而不是 POST

来自分类Dev

jQuery Ajax POST附加到URL

来自分类Dev

Django request.POST.get SQL注入

来自分类Dev

_Request,_Post和_Get修剪空间的缺点?

来自分类Dev

request.POST.get() 为空

来自分类Dev

在Django表单中访问request.GET或request.POST

来自分类Dev

在Django表单中访问request.GET或request.POST

来自分类Dev

@ Ajax.ActionLink使用GET而不是POST

来自分类Dev

GET和POST ajax Android Web视图

来自分类Dev

将JQuery ajax GET转换为POST

来自分类Dev

带有POST和GET的AJAX

来自分类Dev

jQuery Ajax将POST更改为GET

来自分类Dev

How to send json data in url as request parameters in java

来自分类Dev

mvc server-side validation for jquery ajax post request

来自分类Dev

jQuery ajax返回$ _POST而不是REQUEST有效负载

来自分类Dev

Ajax Post Request不一致,为什么?

来自分类Dev

何时在jQuery AJAX中执行$ .ajax或$ .get或$ .post

来自分类常见问题

捕获request.GET中的URL参数

来自分类Dev

request.get(url)返回空内容

来自分类Dev

RESTful API URL最佳实践GET与POST

来自分类Dev

从GET结构的URL发送POST请求

来自分类Dev

django Ajax GET url 未找到

来自分类Dev

python 3.5 urllib.request.Request POST数据到网站。做GET而不是POST

来自分类Dev

Ajax调用后request.url不正确?