1

我以前做过这个,但由于某种原因,参数被奇怪地传递了。

我有一个用于传递参数的 javascript 函数,我运行了一些测试并且在函数中变量是正确的。

这些只是与该问题相关的几个 js 片段:

var tdes = document.getElementById("taskDescription1").value;
var tnam = document.getElementById("taskName1").value;
var shif = document.getElementById("shift1").value;
var ttyp = document.getElementById("taskType1").value;
var date = document.getElementById("datepicker").value;
var ooc = document.getElementById("ooc1").value;
var dateSplit = date.split('/');
var deadlineDate = "";



for( var i = 0; i < dateSplit.length; i++){
deadlineDate = deadlineDate + dateSplit[i]; 
}
xmlhttp.open("GET","subTask.php?q="+ encodeURIComponent(tdes) + "&w=" + encodeURIComponent(tnam) +"&e=" +encodeURIComponent(shif) + "&y=" + encodeURIComponent(ttyp) + "&b=" + encodeURIComponent(deadlineDate) + "&u=" + encodeURIComponent(ooc),true);

我运行了一个 Web 控制台,这实际上是通过了...

http://***************/****/********/subTask.php?taskName1=test+taskname+works&taskDescription1=test+des&shift1=All&ooc1=Open&taskType1=normal&datepicker=06%2F28%2F2013

我不确定 xmlhttp.open 和 php 中的 GET 方法之间发生了什么。这些变量都没有通过。

4

2 回答 2

0

为什么不使用 jQuery - 非常简单的格式(我更喜欢 POST...):

$(document).ready(function() {
    var tdes = $("#taskDescription1").val();
    var tnam = $("#taskName1").val();
    var shif = $("#shift1").val();
    var ttyp = $("#taskType1").val();
    var date = $("#datepicker").val();
    var ooc = $("#ooc1").val();
    var dateSplit = date.split('/');
    var deadlineDate = "";

    for( var i = 0; i < dateSplit.length; i++){
        deadlineDate = deadlineDate + dateSplit[i]; 
    }

    $.ajax({
        type: "POST",
        url: "subTask.php",
        data: "q="+ encodeURIComponent(tdes) + "&w=" + encodeURIComponent(tnam) +"&e=" +encodeURIComponent(shif) + "&y=" + encodeURIComponent(ttyp) + "&b=" + encodeURIComponent(deadlineDate) + "&u=" + encodeURIComponent(ooc),true),
        success: function(whatigot) {
            alert('Server-side response: ' + whatigot);
        } //END success fn
    }); //END $.ajax

}); //END document.ready()

注意success回调函数是多么容易编写...... subTask.php 返回的任何内容都将在该函数中可用,如 alert() 示例所示。

请记住在<head>标签中包含 jQuery 库:

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>

另外,将此行添加到文件顶部subTask.php,以查看发生了什么:

<?php
    $q = $_POST["q"];
    $w = $_POST["w"];
    die("Value of Q is: " .$q. " and value of W is: " .$w);

q=和的值w=将在警告框中返回给您,以便(至少)您可以看到 subTask.php 收到它们时包含的值

于 2013-06-18T21:56:14.790 回答
0

以下脚本应该有帮助:

function ajaxObj( meth, url ) 
{   
var x = false;
if(window.XMLHttpRequest)
    x = new XMLHttpRequest();
else if (window.ActiveXObject) 
    x = new ActiveXObject("Microsoft.XMLHTTP");  
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
    if(x.readyState == 4 && x.status == 200){
        return true;    
    }
}

    var ajax = ajaxObj("POST", "subTask.php");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
           console.log( ajax.responseText )
        }
    }
    ajax.send("u="+tdes+"&e="+tnam+ ...... pass all the other 'n' data );
于 2016-01-18T12:52:25.020 回答