我试图让我连接到一个php文件,但我没有从服务器回复任何响应有什么不对吗?
function request(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "http://people.dsv.su.se/~pierre/courses/05_ass/ip3/3/3.7.1/example.php";
var number1 = document.getElementById("number1").value;
var number2 = document.getElementById("number2").value;
var vars = "?number1="+number1+"&number2="+number2
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("result").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById("result").innerHTML = "processing...";
}
我没有从服务器得到任何答案我做错了吗?
我现在尝试在jquery中编写它,但仍然没有来自服务器的响应
$(document).ready(function() {
// Skicka nummrerna vid klick på #calculate
$('#calculate').click(function() {
var url = "http://people.dsv.su.se/~pierre/courses/05_ass/ip3/3/3.7.1/example.php?";
var number1 = document.getElementById("number1").value;
var number2 = document.getElementById("number2").value;
var numbers = url + number1 + "&" + number2
if (window.XMLHttpRequest)
{// Kod för nya webbläsare
xmlhttp=new XMLHttpRequest();
}
else {//om det inte är en nyare webbläsare
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// Skriv ut svaret från servern i result
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",numbers,true);
xmlhttp.send();
alert(numbers);
});
});
答案 0 :(得分:2)
您正在格式化变量,就像您在GET请求中使用它们一样,然后将请求设置为POST。
[编辑:演示评论]
尝试更改这两行。
hr.open("GET", url + vars, true);
hr.send();
[edit]包含更改的完整原始代码
function request(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "http://people.dsv.su.se/~pierre/courses/05_ass/ip3/3/3.7.1/example.php";
var number1 =22;// document.getElementById("number1").value;
var number2 =22;// document.getElementById("number2").value;
var vars = "?number1="+number1+"&number2="+number2
hr.open("GET", url + vars, true);
// Set content type header information for sending url encoded variables in the request
//hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("result").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(); // Actually execute the request
document.getElementById("result").innerHTML = "processing...";
}
答案 1 :(得分:0)
您需要以数据作为参数发送()您的请求。类似的东西:
params = "number1="+number1+"&number2="+number2;
hr.send(params);
答案 2 :(得分:0)
以下是jQuery格式的完整代码:
$(document).ready(function () {
$('#calculate').click(function () {
var url = "http://people.dsv.su.se/~pierre/courses/05_ass/ip3/3/3.7.1/example.php?number1=" + $("#number1").val() + "&number2=" + $("#number2").val();
$.ajax(url, {
complete: function (data, textStatus, jqXHR) {
//Put the code that handle the response here
$("#result").html(data.responseText);
}
});
});
});