简体   繁体   English

Javascript:postAjax不发布任何内容

[英]Javascript: postAjax doesn't post anything

Good Morning! 早上好!

I am writing a small AJAX application, using this function as a base, and php as serve side language. 我正在编写一个小的AJAX应用程序,使用功能作为基础,而php作为服务辅助语言。

here is the javascript code involved 这是涉及的JavaScript代码

var dati = {};
dati.nome = d.getElementById('nome').value;
dati.cognome = d.getElementById('cognome').value;
console.log(dati);
url = "post.php";
jsonToPost = dati;
console.log(url);

processResponse = function(responseText){
    console.log(responseText);
    d.getElementById('response').innerHTML = responseText;
}
_SU3.postAjax(url, processResponse, jsonToPost);
});

And here is my php code, just a post processor to test ajax 这是我的php代码,只是用于测试ajax的后处理器

<?php
if(isset($_POST['nome'])){

$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$data['nome'] = $nome;
$data['cognome'] = $cognome;

$data = json_encode($data);

echo $data;
} else {
    echo "Errore!";
}
?>

The response I get from the ajaxRequest is "Errore!", it means it's not posting anything to the script. 我从ajaxRequest获得的响应是​​“ Errore!”,这意味着它没有向脚本发布任何内容。

Am I missing any obvious mistake? 我是否遗漏了任何明显的错误? Or there is something i'm doing completely wrong? 还是我做错了什么?

The _SU3.ajax() function (see this for reference) is working just fine, so i'm surprised it doesn't work _SU3.ajax()函数(请参阅此内容以供参考)工作正常,所以我很惊讶它不起作用

---edit to ad the firebug response--- I hope i posted the right thing ---编辑萤火虫回应广告---希望我发布正确的内容

request headers:
POST /~francesco/post.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/2010010 Firefox/16.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost/~francesco/ajax.html
Content-Length: 26
Pragma: no-cache
Cache-Control: no-cache

Screenshot in the screen shot there are other informations. 屏幕快照中的屏幕快照还有其他信息。

You might want to replace the line: 您可能要替换以下行:

ajaxRequest.send("data=" + encodeURIComponent(data));

In _SU3.postAjax to: 在_SU3.postAjax中:

ajaxRequest.send(data);

And then send the third argument (jsonToPost) as a string like this: 然后将第三个参数(jsonToPost)作为字符串发送,如下所示:

"nome=arg1&cognome=arg2"

For example: 例如:

"nome="+d.getElementById("nome").value + "&cognome="+d.getElementById("cognome").value`

As puckipedia suggested, you need to encode the object you are sending as a query string. 正如puckipedia所建议的那样,您需要将要发送的对象编码为查询字符串。 The following method will create the parameter string based on the object passed in: 以下方法将基于传入的对象创建参数字符串:

_SU3.postAjax = function(url, callback, data) {

   var parameters = '';

   for (var p in data){
     parameters  += p + '=' + encodeURIComponent(data[p]) + '&';
   }

   var ajaxRequest = _SU3.getAjaxRequest(callback);
   ajaxRequest.open("POST", url, true);
   ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   ajaxRequest.setRequestHeader("Connection", "close");
   ajaxRequest.send(parameters);
}

The function suggested in the link you provided was assuming you were passing in JSON, not a js object. 您提供的链接中建议的功能是假设您传入的是JSON,而不是js对象。

Note: The server code you posted should interpret the data properly: 注意:您发布的服务器代码应正确解释数据:

$nome = $_POST['nome'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM