0

i have serious difficulties with sending variables to my backend(classic asp). I can't get the elements right.
However, if I get it from a normal HTML FORM I can catch and edit the variables.
Why can't I do that with my AJAX?

My Ajax

  node.forEach((element)=>{ arr.push(element.value); i++;});
  let xhr = new XMLHttpRequest;
  xhr.open('post', '../Update/updateAusgabearten.asp', true);
  xhr.setRequestHeader('Content-Type', 'application/json');
  let test = {"   arr[0], arr[1], "Prozent="+arr[2]}
  xhr.send("ID="+arr[0] + '&' + "Name="+arr[1] + '&' + "Prozent="+arr[2]);


My Backend(Classic Asp)

    Set conn = Server.CreateObject("ADODB.Connection")
    conn.open "DRIVER={SQLite3 ODBC Driver};Database=C:/inetpub/wwwroot/TestDatabase/saver.sqlight3"
    SET rs=Server.CreateObject("ADODB.recordset")

    dim sql
    sql = "UPDATE Ausgabenarten " &_
          "SET Name=" & CStr(request("Name")) & "" &_
          ", prozente=" & CDbl(request("Prozent")) & "" &_
          "WHERE id=" & CInt(request("ID")) & ""
       conn.Execute sql
       conn.close
  End if

I would be very happy if someone would show me how to do this with an example he tested himself.

I would also like to send an array with ajax and receive it from my backend(Classic asp).

Bayo
  • 3
  • 3
  • Needs more info. What errors are you seeing on the client/server when your ajax executes? – kvsm Sep 22 '19 at 19:49
  • 1
    Also, not related to the question at hand, but appending request parameters directly into a SQL query like that will leave your code wide open to SQL injection attacks. See: https://stackoverflow.com/questions/149848/classic-asp-sql-injection-protection – kvsm Sep 22 '19 at 19:50
  • @kvsm I'm not getting any error. But my backend can't do anything with it, my data will neither be inserted nor updated. It seems as if my `backend` can't do anything with `ajax`, because it recognizes and reads the `html From`. – Bayo Sep 22 '19 at 21:10
  • 1
    Indepedently from the fact your asp code is open to SQL injection, something seems wrong with your ajax call. You've got double quotes where they shouldn't be. Since you're only passing 3 arguments, use a GET method instead of a POST, build your url string including parameters and check that it's formed correctly (using alert or console) –  Sep 23 '19 at 06:47
  • 1
    The problem isn't Classic ASP, it's the JavaScript. – user692942 Sep 23 '19 at 06:56
  • 1
    The request content type isn't `application/json` you may want a JSON response but the [content you are sending is `application/x-www-form-urlencoded`](https://stackoverflow.com/a/4073451/692942). That is why you can't pick up the values through the `Request` object. If you want to guarantee a JSON response use the [`accept` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept) `accept: application/json`. – user692942 Sep 24 '19 at 08:37

0 Answers0