My ajax call works only the first time. After that I get an Method not allowed exception.
Here is my java script code:
$('.AppFormFormNewOT').submit(function () {
var formID = $(this).attr('id');
var appDate = $('#' + formID + ' .dateHiddenInput').val();
var appMit = $('#' + formID + ' .mitHiddenInput').val();
var am = $('#' + formID + ' .amCheckBox').attr('checked');
var pm = $('#' + formID + ' .pmCheckBox').attr('checked');
if (!am && !pm) {
alert("Bitte geben Sie einen Halbtag an");
return false;
}
var vorOrNach;
if (am == 'true,false') {
vorOrNach = 'FirstHalfDayHasAppointment';
} else {
vorOrNach = 'SecondHalfDayHasAppointment'
}
var url = 'Plan/' + vorOrNach + "/";
var occupied = true;
$.ajax({
type: 'POST',
url: url,
datatype: 'text',
async: false,
data: {
date: appDate,
id_person: appMit
},
success: function (data) {
occupied = data;
if (occupied == "True") {
alert("Hier ist bereits ein Termin");
return false;
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('Ajax error');
alert(textStatus);
alert(errorThrown);
return false;
}
});
if (occupied == "True" || occupied == true) {
return false;
} else {
return true;
}
});
The method it calls are simple:
[ScriptMethod][HttpPost]
public string FirstHalfDayHasAppointment(DateTime date, int id_person)
{
bool hasApp = repo.dateWithPersonAlreadyHasApp(date, id_person, HalfDay.AM);
return hasApp.ToString();
}
[ScriptMethod][HttpPost]
public string SecondHalfDayHasAppointment(DateTime date, int id_person)
{
bool hasApp = repo.dateWithPersonAlreadyHasApp(date, id_person, HalfDay.PM);
return hasApp.ToString();
}
When I restart the application (not just reloading the page), again the first call works and any following calls fail again.
edit: When i just return true in the functions the ajax calls always succeed:
[ScriptMethod][HttpPost]
public string FirstHalfDayHasAppointment(DateTime appDate, int id_person)
{
return "True";
}
[ScriptMethod][HttpPost]
public string SecondHalfDayHasAppointment(DateTime appDate, int id_person)
{
return "True";
}
So the problem seems to be on the server side. But I don’t know what this has to do with ajax and why ajax returns a method not allowed exception. Hope this information is useful. Thanks for your help.
I changed the name of the variable ‘url’ to another name, for example ‘myurl’ and I worked. It seems the variable ‘url’ has some predefined value.