Hei everyone. i have project to make communication between my website and my application using web api. i have success to get data from api controller in my application and browser (via url). but i can't update the data in my website. for example: i write a text on input box and click the write button. when i click read button the data not change.
the data i want to update is string data. here is my code
Web Api with read/write template
public class ActionController : ApiController
{
public string text1 = "Read Data Success";
// GET: api/Action
public string Get()
{
return text1;
}
// GET: api/Action/5
public string Get(int id)
{
return "value" + id;
}
// POST: api/Action
public void Post([FromBody]string value)
{
text1 = value;
}
// PUT: api/Action/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/Action/5
public void Delete(int id)
{
}
}
Html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Server Manager</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="shortcut icon" href="favicon.ico" />
<script src="Scripts/Action.js"></script>
</head>
<body>
<div>
<input id="Button1" type="button" value="write" />
<input id="Button2" type="button" value="read" />
<input id="TextIn1" type="text" />
<p id="text1">Text 1</p>
</div>
</body>
</html>
Jquery
$(document).ready(function () {
$("#Button1").click(function () {
var TextIn = $("#TextIn1").val();
$.ajax({
type: 'POST',
url: 'api/Action',
dataType: 'text',
data: TextIn,
success: function (response) {
alert("Writed " + response);
},
error: function (response) {
alert(response.responseText);
}
});
console.log("write complete");
});
$("#Button2").click(function () {
$.ajax({
url: 'api/Action',
type: 'GET',
dataType: 'json',
success: function (data, textStatus, xhr) {
$("#text1").text("Get " + data);
},
error: function (responese) {
alert(responese.responseText);
}
});
console.log("read complete");
});
});
i know my code looks weird or ambiguous. because this is my first time in web programming