【问题标题】:Passing data from javascript to php using ajax?使用ajax将数据从javascript传递到php?
【发布时间】:2016-02-02 10:13:38
【问题描述】:

我正在尝试从我的 JavaScript 中检索此数据 speedMbps。使用 Ajax 将数据发布到我的 php 代码,但我没有得到任何输出。我是 ajax 新手,只使用 ajax 来自动完成。

<script src="//code.jquery.com/jquery-1.10.2.js"></script>          

<script>
//JUST AN EXAMPLE, PLEASE USE YOUR OWN PICTURE!
var imageAddr = "testimage.jpg"; 
var downloadSize = 2097152; //bytes

window.onload = function() {
    var oProgress = document.getElementById("progress");
    oProgress.innerHTML = "Loading the image, please wait...";
    window.setTimeout(MeasureConnectionSpeed, 1);
};

function MeasureConnectionSpeed() {
    var oProgress = document.getElementById("progress");
    var startTime, endTime;
    var download = new Image();
    download.onload = function () {
        endTime = (new Date()).getTime();
        showResults();
    }

    download.onerror = function (err, msg) {
        oProgress.innerHTML = "Invalid image, or error downloading";
    }

    startTime = (new Date()).getTime();
    var cacheBuster = "?nnn=" + startTime;
    download.src = imageAddr + cacheBuster;

    function showResults() {
        var duration = (endTime - startTime) / 1000;
        var bitsLoaded = downloadSize * 8;
        var speedBps = (bitsLoaded / duration).toFixed(2);
        var speedKbps = (speedBps / 1024).toFixed(2);
        var speedMbps = (speedKbps / 1024).toFixed(2);
        oProgress.innerHTML = "Your connection speed is: <br />" + 
           speedBps + " bps<br />"   + 
           speedKbps + " kbps<br />" + 
           speedMbps + " Mbps<br />";

        $.ajax({
          method: "POST",
          url: "test_select.php",
          data: {speedMbps: speedMbps },
          cache: false
        }).done(function( html ) {
            $( "#speed" ).val( html );
        });
    }
}

</script>

<input type="text" id="speed" name="speed" value="">

test_select.php

<?php
    echo $_POST['speedMbps'];

    if(isset($_POST['speedMbps'])){
        echo $speedMbps = $_POST['speedMbps'];
    }

?>

【问题讨论】:

  • 尝试在您的 AJAX 调用中设置 method: "POST"
  • 您的 speedMbps 变量不在您的 AJAX 调用范围内。
  • 在你的 ajax 代码中声明的speedMbps 在哪里?
  • 我刚刚尝试了您更新的代码,它对我来说是有效的。你还有同样的问题吗?如果是这样,您是否检查过浏览器的控制台?出现任何错误?

标签: javascript php jquery html ajax


【解决方案1】:
$.ajax({
      type: "POST",
      url: "test_select.php",
      data: {speedMbps: speedMbps },
      cache: false
    }).done(function( html ) {
        $( "#results" ).val( html );
    });

method:"POST" 更改为type:"POST"

【讨论】:

  • 尝试添加选项 dataType:"text" 或只是 echo 1; 并检查输出
  • 方法或类型做同样的事情。
【解决方案2】:

您可能希望将顶部脚本合并到底部脚本并将其放在您的 jquery 钩子之后,因为变量。

据我所知,您的 Ajax 脚本中还缺少 method: "POST"。这需要指定 HTTP 请求的类型,因为您在 .php 文件中使用 $_POST,请求类型需要匹配。

$.ajax({
  method: "POST",
  url: "test_select.php",
  data: {speedMbps: speedMbps },
  cache: false
}).done(function( html ) {
    $( "#results" ).val( html );
});

这里是关于 ajax 的 jQuery 文档:ajax docs

【讨论】:

  • 我合并了我的脚本,但仍然没有得到任何输出。请帮忙。
猜你喜欢
  • 1970-01-01
  • 2020-08-09
  • 2014-10-21
  • 2019-11-16
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 2011-04-29
相关资源
最近更新 更多