我有一个文件打算通过Ajax发送数据到服务器,我已经尝试了一些库,但我不能让它们工作,所以我尝试在服务器文件中简单的Request.Form()方法,也不起作用。
Ajax帖子:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://localhost/serv/sync.asp", true);
DataToSend = "id=1";
xmlhttp.addEventListener("load", function () {
if(xmlhttp.status === 200){
//event handler
};
}, false);
xmlhttp.send(DataToSend);ASP文件:
<%@language=vbscript%>
<%
value = Request.Form("id")
Response.ContentType = "text/xml"
response.write (value)
%>这有什么问题呢?我已经在控制台中检查了帖子,它正在工作,但我无法捕获服务器端的值。
最初的想法是发送一个Json字符串,在服务器中解析它,然后执行dataBase插入,但是无法让它工作,有谁有一个工作的代码片段或到Classic ASP中工作的Json解析方法的链接?谢谢。
注意:由于线程问题,我尝试将服务器文件更改为其他文件夹,并将URL更改为"http://127.0.0.1/serv/sync.asp"“。
发布于 2012-12-21 19:52:28
我已经成功地使用了这一点:
JS:
if (window.XMLHttpRequest) {
httprequest = new XMLHttpRequest();
httprequest.texto = busca.id;
} else if(window.ActiveXObject) {
httprequest = new ActiveXObject("Microsoft.XMLHTTP");
httprequest.texto = busca.id;
} else {
alert("Seu navegador não suporta Ajax.");
return false;
}
if (httprequest.readyState == 4 || httprequest.readyState == 0) {
var busca = escape("texto texto texto");
httprequest.open("POST", "../busca_ajax.asp", true);
httprequest.onreadystatechange = retornaValores;
httprequest.send("busca=" + busca + "&teste=2");
}
function retornaValores() {
if (httprequest.readyState == 4) {
alert(httprequest.responseText);
}
}ASP:
dim busca
busca = trim(request("busca"))
response.write busca编辑:
如果可以,我建议您使用jQuery。它大大简化了这个过程:
$.ajax({
url: "lista.asp",
data: { 'ajax': 's', 'dados': '{"id": 123, "nome":"teste"}'},
cache: false,
dataType: "json",
success: function(dados) {
alert(dados);
},
error: function() {
alert("ERRO!");
}
});ASP:
dim ajax, id
ajax = request.form("ajax")
dados = request.form("dados") ' this is a JSON string
response.write dados https://stackoverflow.com/questions/13988993
复制相似问题