簡體   English   中英

如何使用get JSON將參數傳遞給php

[英]How to pass parameters to php using get JSON

我有一個json_data.php文件,我想使用getJSON將參數傳遞給switch語句內的函數。

<?php
 require_once "Db.php";
 $db = new Db();

 if(isset($_GET['method'])) {
    switch (($_GET['method'])) {
       case 'getUserIds':
          echo json_encode($db -> getUserIds());
          exit();

       case 'getUser':
          echo json_encode($db -> getUser($userId) {
          exit();



       // other cases go here
       case 'getCertCategories':
          echo json_encode($db -> getCertCategories());
          exit();

       case 'get
       default:
          break;
    }

 }

如何傳遞參數,以便可以將$userId傳遞給getUser函數?

$.getJSON的第二個參數是一個包含查詢參數的對象。

$.getJSON('json_data.php', { method: 'getUserIds' }, function(response) {
    ...
});

$.getJSON('json_data.php', { method: 'getUser', userId: 'joe' }, function(response) {
    ...
});

在PHP中,您將訪問$_GET['method']$_GET['userId']等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM