Hey there! I'm currently working on my computer science project for school, using asp.net and c#. I've run into a bit of an issue where my AJAX POST request is being received as a GET request. I've tried to troubleshoot it myself but no luck, so I could really use some help :)
The problem occurs when I try to send an AJAX POST request upon changing a textbox, intending to write its content to a file in the backend. However, when I check for Request.HttpMethod, it shows as a GET request.
Here's a snippet of my HTML code:
<%@ Page Async="true" Language="C#" AutoEventWireup="true" CodeBehind="FilePage.aspx.cs" Inherits="filmhelperschoolproject.Pages.FilePage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<body>
<center>
<form runat="server" id="form1">
<div>
<textarea style="width:40%; height:800px;" runat="server" id="fileTextArea" oninput= "saveText()"></textarea>
</div>
</form>
</center>
</body>
<script>
function saveText() {
event.preventDefault();
var url_string = window.location.href;
var url = new URL(url_string);
var name = url.searchParams.get("name");
var id = url.searchParams.get("id");
var filename = name + id;
var text = document.getElementById('fileTextArea').value;
$.ajax({
method: "POST",
url: "FilePage.aspx?id=" + id + "&name=" + name,
data: {
"text": "hello world"
},
dataType: "html",
success: function (response) {
console.log('File written successfully!');
},
error: function (xhr, status, error) {
console.log('Error writing file: ' + error);
}
});
}
</script>
</html>
Here's a snippet from my Page_Load function:
protected void Page_Load(object sender, EventArgs e)
{
string name = Request.QueryString["name"];
string id = Request.QueryString["id"];
string path = "C:\\Users\\User\\source\\repos\\filmhelperschoolproject\\filmhelperschoolproject\\Files\\";
if (Request.HttpMethod == "POST")
{
string text = Request.Form["text"];
File.WriteAllText(path + name + id + ".txt", text);
return;
}
if (!IsPostBack)
{
fileTextArea.InnerText = "";
string [] lines=File.ReadAllLines(path + name + id + ".txt");
foreach( string line in lines)
{
fileTextArea.InnerText += line + "\n";
}
}
}
Your help would be greatly appreciated!
(P.S. You're looking very sharp today, sir!)