Within a JavaScript function, I am making a typical ajax call:
$.ajax({
type: 'GET',
url: '@Url.Content("~/Incident/LookUpStudent")',
data: { userInput: userInputStudentLDN },
success: function (result) { //doing stuff here}
});
This call directs to the following method in the controller:
public string LookUpStudent(string userInput)
{
if (string.IsNullOrWhiteSpace(userInput)) { return "Student not found. Please try again."; }
try
{
var fetchedData = new WebClient().DownloadString(JSONUrl); //download the data from the URL
}
//return statement and other stuff here
}
The issue arises when I reach this part of the code:
new WebClient().DownloadString(JSONUrl);
Although the fetchedData is retrieved successfully, my current page immediately receives a 404 error. This added "Action" at the end of the URL triggers the error. Here is the specific error message:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Incident/Action
I'm puzzled as to why this additional "Action" is appended to the URL and causing the 404 error. How can I resolve this?
Based on Kosala W's answer, I made the following edit:
var req = { userInput: userInputStudentLDN };
$.get('@Url.Content("~/Incident/LookUpStudent")', { userInput: req }, function (result) {
//result is your response from the controller
});