【问题标题】:How to transform this JavaScript in URL如何在 URL 中转换此 JavaScript
【发布时间】:2014-07-01 23:59:46
【问题描述】:

我有这个代码:

function name() {
    $.ajax({
      type: "POST",
      url: "/index.php",
      data: { id:  $("#id").val(),  date: new Date().getTime()}, 
    }); 
}

我想了解在 URL 中的翻译是什么。我已经尝试了一切,但它不起作用。在我看来,应该是index.php?id= ... 但不是。

【问题讨论】:

  • 这是使用POST 方法。所以你不能只是在浏览器的 url 栏中放一些东西并得到相同的结果。如果这使用了GET 方法,那么可以确定。如果 index.php 页面仅设置为处理 POST 请求,则向其发送 GET 请求将无济于事。
  • 正如@Chausser 所说,您正在使用 post 方法。使用 get 方法,url 看起来像 /index.php?id=foo&date=bar
  • 所以我不能用 post 方法得到我要找的东西?

标签: javascript php url


【解决方案1】:

另一种选择是:

function name() {
    $.get('index.php?id=' + $("#id").val() +'&date ='+ new Date().getTime() +'',function(data){
        //work with the data callback here.
    });
}

【讨论】:

    【解决方案2】:

    试试这个:

    function name() {
        $.ajax({
          type: "GET",
          url: "/index.php",
          data: { id:  $("#id").val(),  date: new Date().getTime()}, 
        }); 
    }
    

    并使用 $_GET['id'] , $_GET['date']... 接收 id 和日期...

    如果你使用 POST 类型,你必须用 $_POST 接收。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 2020-07-24
      • 2013-07-27
      • 2017-10-21
      • 2014-02-12
      相关资源
      最近更新 更多