0

I'm trying to save a few lines of text in a textarea with ajax targeting a classic asp file.

I'm not sure how to use ajax when when it comes to sending data with POST method and NOT using jQuery, didn't find any questions concerning this here either, no duplicate intended.

Ajax function:

function saveDoc() {//disabled
var xhttp = new XMLHttpRequest();
var note = document.getElementById("note");

xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        document.getElementById("0").innerHTML = xhttp.responseText;
    }
};
xhttp.open("POST", "saveNote.asp", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(note);

ASP Classic:

set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.OpenTextFile("c:\inetasp\1.txt",8,true)

dim note

note = RequestForm("note")
f.Write(note)
f.Close
Response.Write("Works.");

set f=nothing
set fs=nothing

I'm aware there might be a lot wrong with the .asp since i couldn't find any specific info about how to handle ajax requests with Classic ASP correctly.

Any suggestions on how to make this work without jQuery are welcome.

Karl Kuusik
  • 77
  • 10

1 Answers1

0

I cannot test your code as I don't have a backend running on my machine right now. But I can already tell you a few things:

  • you are calling xhttp.send(note); but your note is a DOM element. It should be a string with a querystring format.
  • in your server side code you call RequestForm is it a custom function you have previously defined ? The usual syntax is Request.Form

Hope it can help

  • Fixed the problems you pointed out by changing the note variable to document.getElementById("note").value.toString() and changing the type-o of Request.Form, but i still get the same HTTP Error 405.0 - Method Not Allowed. – Karl Kuusik Jan 31 '16 at 17:43
  • Ok... HTTP error 405 means that the method you are using to request a resource is not allowed for this resource. In your case, you can't POST to saveNote.asp. Have a look at the following links http://stackoverflow.com/questions/1016008/classic-asp-error-405 , https://www.webmasterworld.com/forum23/1678.htm , http://www.somacon.com/p126.php –  Jan 31 '16 at 20:08
  • 1
    A few things to try: xhttp.send("note=" + note.value); Are you sure you're actually hitting saveNote.asp file, does it work with a GET request? – Thomas Kjørnes Feb 01 '16 at 00:03
  • 1
    also change `OpenTextFile("c:\inetasp\1.txt"...` to `OpenTextFile(Server.MapPath("1.txt")....` . Ther might be some permission issues in the original path – Flakes Feb 01 '16 at 09:58
  • Regadless of the `Server.MapPath`, the eventual permission issues will still be on. Try to access the .txt from the browser; If it throws an `Unauthorized` error, go to the Security tab in the file, then add a Write permission for the IUSR user. – Eric Wu Feb 01 '16 at 16:55