I have a simple, one-field html form of which I'm posting the value to a server side script (written in Razor) using AJAX. The form user has to guess a particular value and will receive a message depending if it's correct or not.
Is it possible to return a message using the method described below? Ideally I would like to return the variable "message" from my server-side script and display the value to the user on the front end.
My HTML:
<form>
<input type="text" id="value" name="value" />
<button>Send</button>
</form>
My Ajax call:
$(document).on("click", "button", function(e){
e.preventDefault();
var dataString = "value=" + $("#value").val();
$.ajax({
type: "POST",
url: "/post",
data: dataString,
success: function(data) {
alert(data.returnedValue);
}
});
});
My server-side script:
if(IsPost)
{
string value = Request.Form["value"];
string message = "";
if(value.ToString() == "CorrectAnswer"){
message = "Correct!";
return message;
}else{
message = "Wrong!";
return message;
}
}
If your server page is an Asp.net page (.aspx), you probably need to use Response.Write
string value = Request.Form["value"];
string message = "";
if(value.ToString() == "CorrectAnswer"){
message = "Correct!";
return message;
}else{
message = "Wrong!";
}
Response.Write(message );
Also, This is not going to be a postback event. So you do not need to check that.
and in your script,
success: function(data) {
alert(data);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.