Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用POST将字符串发送到php_Javascript_Ajax - Fatal编程技术网

Javascript 如何使用POST将字符串发送到php

Javascript 如何使用POST将字符串发送到php,javascript,ajax,Javascript,Ajax,我使用以下代码通过javascript中的xmlhttp发送字符串: function SendPHP(str, callback) { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new

我使用以下代码通过javascript中的xmlhttp发送字符串:

    function SendPHP(str, callback) {
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {


                         callback(xmlhttp.responseText); //invoke the callback
        }
    }
        xmlhttp.open("GET", "testpost.php?q=" + encodeURIComponent(str), true);

    xmlhttp.send();

}
还有一些测试php:

$q = $_GET['q'];
echo $q;
在我开始发送一个更大的字符串之前,这种方法工作得很好,在这种情况下,我得到了“HTTP/1.1 414请求URI太长”错误

经过一番研究,我发现我需要用“POST”来代替。所以我把它改成:

xmlhttp.open("POST", "sendmail.php?q=" + str, true);
以及:

但在使用POST时,这不会产生任何反响。我怎样才能修复它,使它像我使用GET时那样工作,但又能处理大量数据

编辑我现在正在尝试:

function testNewPHP(str){


    xmlhttp = new XMLHttpRequest();

str = "q=" + encodeURIComponent(str);

  alert (str);

xmlhttp.open("POST","testpost.php", true);
xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState == 4){
     if(xmlhttp.status == 200){
                            alert (xmlhttp.responseText);
     }
    }
  };
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(str);

}

您不应该向href提供URL参数,而应该
send()
send()。此外,您应该始终使用
encodeURIComponent()
对参数进行编码(至少在您的请求使用
内容类型时使用
“application/x-www-form-urlencoded”

您的javascript函数:

function testNewPHP(){
var str = "This is test";

xmlhttp = new XMLHttpRequest();

str = "q=" + encodeURIComponent(str);

alert (str);

xmlhttp.open("POST","testpost.php", true);
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState == 4){
        if(xmlhttp.status == 200){
            alert (xmlhttp.responseText);
        }
    }
};
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(str);

}

您不应该向href提供URL参数,而应该
send()
send()。此外,您应该始终使用
encodeURIComponent()
对参数进行编码(至少在您的请求使用
内容类型时使用
“application/x-www-form-urlencoded”

您的javascript函数:

function testNewPHP(){
var str = "This is test";

xmlhttp = new XMLHttpRequest();

str = "q=" + encodeURIComponent(str);

alert (str);

xmlhttp.open("POST","testpost.php", true);
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState == 4){
        if(xmlhttp.status == 200){
            alert (xmlhttp.responseText);
        }
    }
};
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(str);

}
javascript:

 function testNewPHP(){
var str = "This is test";

xmlhttp = new XMLHttpRequest();

str = "q=" + encodeURIComponent(str);

alert (str);

xmlhttp.open("POST","testpost.php", true);
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState == 4){
        if(xmlhttp.status == 200){
            alert (xmlhttp.responseText);
        }
    }
};
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(str);

}
主目录中的testpost.php:

<?php
 var_dump($_POST);
javascript:

 function testNewPHP(){
var str = "This is test";

xmlhttp = new XMLHttpRequest();

str = "q=" + encodeURIComponent(str);

alert (str);

xmlhttp.open("POST","testpost.php", true);
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState == 4){
        if(xmlhttp.status == 200){
            alert (xmlhttp.responseText);
        }
    }
};
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(str);

}
主目录中的testpost.php:

<?php
 var_dump($_POST);


您不使用url参数发布变量,它们是通过url参数发送的,我基本上是使用它将一个CSV文件转换成字符串发送到我的php脚本,该脚本对其进行一些处理。所以它只是一个需要php脚本处理的长字符串。请参阅此链接->@Saman我之前读过,以确定我需要使用POST而不是GET,但它没有说明如何使用它与GET的区别。您不会使用url参数发布变量,它们是通过发送的,我基本上是用它将一个CSV文件转换成字符串发送到我的php脚本,php脚本对其进行一些处理。所以它只是一个需要php脚本处理的长字符串。请参阅此链接->@Saman我之前读过,以确定我需要使用POST而不是GET,但它没有说明如何使用它和GET的区别。@tadman:我刚刚编辑了它。快!!不能让那个错误妨碍你的回答。我代码中的str字符串是CSV文件的内容。在这个答案中,它被什么覆盖了吗?我不确定我是否理解json部分。对不起,这是我的代码中的一个块,我已经更新了itAh。谢谢,我会尝试一下now@tadman:我刚刚在编辑那东西。快!!不能让那个错误妨碍你的回答。我代码中的str字符串是CSV文件的内容。在这个答案中,它被什么覆盖了吗?我不确定我是否理解json部分。对不起,这是我代码的一个块,我已经更新了itAh谢谢,我现在就试试哦,哇,太棒了!非常感谢!到底出了什么问题?我还没有搞定是什么问题,你能吗?)酷,搞定!哈哈,不知道最后出了什么问题。谢谢你,老兄,你是个传奇!哦,哇,太棒了!非常感谢!到底出了什么问题?我还没有搞定是什么问题,你能吗?)酷,搞定!哈哈,不知道最后出了什么问题。谢谢你,老兄,你是个传奇!