Use Jquery to get json data from the server-side php page

Server-side:

$bookmarkarray = array();
if(mysqli_connect_error())
{
    echo "failed to connect to the database : %s\n".mysqli_connect_error();
    exit();
}
if ($getdata = $dbc->query("select * from bookmark"))
{

    while($obj = $getdata->fetch_object())
    {
        //echo $obj->username. " ".$obj->bm_URL."<br/>";
        $temp_bookmark = new bookDB($obj->username,$obj->bm_URL);
        $bookmarkarray[] = $temp_bookmark;
    }

    //convert to json
    if (!empty($bookmarkarray)) {
      
        echo json_encode($bookmarkarray);
    }
   
    $getdata->close();
    $dbc->close();
}

</pre><pre name="code" class="php">class bookDB
{
    public $username ="";
    public $bm_URL="";

    function __construct($_username,$_bmURL)
    {
        $this->username=$_username;
        $this->bm_URL=$_bmURL;
    }
}



Client-side:

<script type="text/javascript">
      $.ajax({
                  // The URL for the request
                  url: "http://localhost:63343/PhpBooks/json_book.php",

                  // The data to send (will be converted to a query string)
                  data: {
                      id: 123
                  },

                  // Whether this is a POST or GET request
                  type: "GET",

                  // The type of data we expect back
                  dataType : "json",
              })
              // Code to run if the request succeeds (is done);
              // The response is passed to the function
              .done(function( json ) {
                  $.each(json,function(idx,value){
                      $("#list").append("<li>" + value.username + "-" + value.bm_URL  + "</li>");
                  });
              })
              .fail(function( xhr, status, errorThrown ) {
                  alert( "Sorry, there was a problem!" );
                  console.log( "Error: " + errorThrown );
                  console.log( "Status: " + status );
                  console.dir( xhr );
              })
              // Code to run regardless of success or failure;
              .always(function( xhr, status ) {
                  alert( "The request is complete!" );
              });
  </script>


Client-side(POST):


<form id="myForm" action="http://localhost/PhpBooks/json_book.php" method="post">
          <p>Name:<input name="name" type="text" id="txtname"/></p>
          <input type="submit" value="Submit"/>
      </form>


<script type="text/javascript">
          var frm = $("#myForm");
          frm.submit(function(event){
              if($("#txtname").val().length===0){

                  alert('Please input a name!');
                  event.preventDefault();
              }
              else
              {

                  //show indicator:
                  $(".loadingindicator").css("visibility","visible");
                  $.ajax({
                              // The URL for the request
                              url: frm.attr("action"),

                              // The data to send (will be converted to a query string)
                              data: frm.serialize(),

                              // Whether this is a POST or GET request
                              type: frm.attr("method"),

                              // The type of data we expect back
                              dataType: "json",
                          })
                          // Code to run if the request succeeds (is done);
                          // The response is passed to the function
                          .done(function (json) {
                              $.each(json, function (idx, value) {
                                  $("#list").append("<li>" + value.username + "-" + value.bm_URL + "</li>");
                              });
                          })
                          .success(function(data){
                              alert('ok');
                          })
                          .fail(function (xhr, status, errorThrown) {
                              alert("Sorry, there was a problem!");
                              console.log("Error: " + errorThrown);
                              console.log("Status: " + status);
                              console.dir(xhr);
                          })
                          // Code to run regardless of success or failure;
                          .always(function (xhr, status) {
                              alert("The request is complete!");
                              $(".loadingindicator").css("visibility","hidden");
                          });
             }
          event.preventDefault();
          });

  </script>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于 json-server-auth 的使用,你可以按照以下步骤进行操作: 1. 首先,安装 json-server-auth 包。你可以使用 npm 进行安装,运行以下命令: ``` npm install json-server-auth ``` 2. 创建一个 JSON 数据文件,用于存储用户信息和受保护的资源。该文件可以是一个独立的 JSON 文件,也可以是一个包含 JSON 数据的 JavaScript 文件。 3. 在你的项目中创建一个新的 JSON Server 实例,并使用 json-server-auth 包装它。你可以在你的 JavaScript 代码中这样做: ```javascript const jsonServer = require('json-server'); const auth = require('json-server-auth'); const server = jsonServer.create(); const router = jsonServer.router('path/to/your/data.json'); const middlewares = jsonServer.defaults(); server.use(middlewares); server.db = router.db; server.use(auth); server.use(router); server.listen(3000, () => { console.log('JSON Server is running'); }); ``` 4. 运行你的服务器。在终端中执行以下命令: ``` node your-server-file.js ``` 5. 现在,你的 JSON Server 已经具备了基本的身份验证功能。你可以使用用户的用户名和密码进行身份验证,并限制对受保护资源的访问。 请注意,json-server-auth 提供了一些默认的身份验证和授权路由,你可以根据自己的需求进行自定义设置。你可以在 json-server-auth 的文档中找到更多的配置和用法示例。 希望这能对你有所帮助!如有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值