【问题标题】:Get the response from server in JSON format without using database?在不使用数据库的情况下以 JSON 格式从服务器获取响应?
【发布时间】:2023-03-06 09:34:01
【问题描述】:

我是服务器端编程的新手,我有一个form,需要采取的措施是,当用户填写姓名、地址和 PIN 码时,提交从然后页面应该加载(就像从提交本地一样)和新数据应该显示(数据在 JSON 文件中可用)。

我在服务器上有 JSON 文件。是否可以在不使用数据库的情况下以 JSON 格式从服务器获取响应??

HTML:---

<form action="https://domain.com/jsonfilelocation/json.json">
    <input type="text" name="name" />
    <input type="text" name="address" />
    <input type="text" name="pincode" />
    <input type="submit" name="submit" />        
</form>

JSON:--- json.json 位于https://domain.com/jsonfilelocation/json.json

{
    "name": "kk",
    "address": "XYZ, New Delhi",
    "pincode": "1000001"
}

【问题讨论】:

  • 为什么我认为你的 json 文件什么都不做,却有静态 json 格式的数组
  • @RoyalBg:这只是一个解释的例子,我有所有可用 JSON 格式的数据!
  • 您可以通过 ajax 请求它,以在当前页面中显示响应,否则您将发送用户打开 .json 文件 :) 我宁愿在另一个 .php 文件中请求该文件并回显响应,因为我不熟悉向 .json 文件发送请求
  • 您不想在某处存储用户的条目吗?如果不是,为什么要将其发送到服务器?也许退后一步,描述一下用例。

标签: javascript php ajax json jquery


【解决方案1】:

您可以将 JSON 字符串存储在本地变量服务器端,并将其作为客户端 AJAX 调用的结果返回,而无需访问数据库。

【讨论】:

    【解决方案2】:
    <?php
    session_start();
    
    //Variables
    $ext    = '.json';
    $me     = isset($_SESSION['me']) ? $_SESSION['me'] : $_SESSION['me'] = rand();
    $file   = $me . $ext;
    
    //If we have a post handle our data, write to a json file.
    if ($_POST) {
        //Don't need the submit key in our data.
        unset($_POST['submit']);
    
        //Write to the file.
        $str    = json_encode($_POST);
        $fp     = fopen($file, 'w') or die("can't open file");
        fputs($fp, $str);
        fclose($fp);
    }
    
    //Check if there is a file with our session name.
    if (file_exists($file)) {
        //Get the file content and json decode it.
        $json   = file_get_contents($file);
        $values = json_decode($json);
    }
    ?>
    <!-- Form with pre-populated values, if they are set -->
    <form action=""method="post">
        <input type="text" name="name" value="<?php print isset($values->name) ? $values->name : ''; ?>" />
        <input type="text" name="address" value="<?php print isset($values->address) ? $values->address : ''; ?>"/>
        <input type="text" name="pincode" value="<?php print isset($values->pincode) ? $values->pincode : ''; ?>"/>
        <input type="submit" name="submit"/>
    </form>
    

    由您决定是否保存困难。但我认为这应该让您对可能性以及如何设置有一个很好的了解。

    【讨论】:

      【解决方案3】:

      如果您不介意使用我强烈推荐的 JQuery 库,也许您正在寻找的是 JQuery getJSON

      它基本上使用 AJAX 来请求 json 文件并对其进行解析:

      $.getJSON('https://domain.com/jsonfilelocation/json.json', function(data) {
          // 'data' is your object containing the parsed JSON data
          var new_name = data.name
          var new_address = data.address
          var new_pincode = data.pincode
          // ...
      

      阅读我给你的文档链接,写得很好。

      【讨论】:

        【解决方案4】:

        在提出请求时只需请求页面https://domain.com/jsonfilelocation/json.json

        【讨论】:

          猜你喜欢
          • 2016-11-13
          • 1970-01-01
          • 1970-01-01
          • 2017-11-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多