Reputation: 574
I have a file intended to send data to server via Ajax, I've tried some libraries but I cant get them to work so I´m trying the simple Request.Form() method in the ASP server file, not working either.
the Ajax post:
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);
the ASP file:
<%@language=vbscript%>
<%
value = Request.Form("id")
Response.ContentType = "text/xml"
response.write (value)
%>
Wat's the problem with this? I've checked the Post in console and its working, but I can't catch the values on the server side.
The original idea was to send a Json string, parse it in the server and do the dataBase inserts, but couldn't get it to work, does anyone have a working snippet or a link to a working Json parsing method in Classic ASP? Thanks.
Note: I've tried changing the server file to a different folder due to threading issues, and changing the URL to "http://127.0.0.1/serv/sync.asp".
Upvotes: 0
Views: 645
Reputation: 16456
I've used this with success:
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
Edit:
if you can, I recommend you to use jQuery. It eases the process a lot:
$.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
Upvotes: 1