I'm making an ajax call to a webmethod and I'm getting the error "unexpected Token >" I'm not sure why I'm getting this.. Any ideas?
function addToQueue(me) {
if (validateTimeFrame()) {
$.ajax({
method: "POST",
url: "GenerateReportModal.aspx/AddToPrintQueue?id=" + $('#patientNum').val(),
data: {buttonID: me.id},
dataType: "json",
success: function (data, textStatus, jqXHR) {
alert(data);
$('#GenerateReportModal').dialog('close');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown + ' ' + textStatus);
console.log(jqXHR);
$('#GenerateReportModal').dialog('close');
}
});
}
}
<----------------------------------------------->
[WebMethod]
public string AddToPrintQueue(string buttonID)
{
try
{
var deIdentify = 0;
var adminUserNum = Int32.Parse(Session["AdminUserNum"].ToString());
if ((Session["AdminUserNum"].ToString() == "6460" || Session["AdminUserNum"].ToString() == "6537" || Session["AdminUserNum"].ToString() == "7885") && (Session["ClinicalDataAdmin"].ToString() == "1" || Session["AccountAdmin"].ToString() == "1" || Session["SystemAdmin"].ToString() == "1"))
deIdentify = (cbDeIdentify.Checked == true) ? 1 : 0;
else
deIdentify = 0;
switch (buttonID)
{
case "FollowUpAddtoQueue":
FollowUpAddToQueueClass(deIdentify);
break;
case "WearTimeAddToQueue":
WearTimeAddToQueueClass(deIdentify);
break;
case "TrendsAddToQueue":
TrendsAddToQueueClass(deIdentify);
break;
case "EndOfUseAddToQueue":
EndofUseAddToQueueClass(deIdentify);
break;
}
}
catch
{
return "There was an issue, we appologize for the inconvenience";
}
return "Added to print queue";
}
<---------------------------------------------------->
<asp:Button runat="server" OnClientClick="return addToQueue(this); return false;" CssClass="button" ID="EndOfUseAddToQueue" Width="150" Text="<%$ Resources:PatientDetail, btnModAddtoQueue %>" />
You're returning a string from your Webmethod, but you've told jQuery to expect JSON.
$.ajax({
//...
dataType: "json",
//...
});
Change that to text (or take it out completely) and see if that solves your problem. Refer to the dataType
property of the jQuery AJAX documentation.
dataType: "json"
, asks you to return json
type from webmethod
!! So you need to decorate your webmethod
with below line
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] //Add this
public string AddToPrintQueue(string buttonID)
{
try
{
//Your other code
}
catch
{
return "There was an issue, we appologize for the inconvenience";
}
return "Added to print queue";
}
In addition to this try specifying contentType
in your ajax
call as below:
$.ajax({
method: "POST",
url: "GenerateReportModal.aspx/AddToPrintQueue?id=" + $('#patientNum').val(),
data: {buttonID: me.id},
contentType: "application/json; charset=utf-8", //Add this line too
dataType: "json",
success: function (data, textStatus, jqXHR) {
alert(data);
$('#GenerateReportModal').dialog('close');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown + ' ' + textStatus);
console.log(jqXHR);
$('#GenerateReportModal').dialog('close');
}
});
I think WebMethod doesn't accept query string parameter. You should try to call webmethod by only post method.
One quickie might be related to the question itself or might just be an tip on the road.
This line: data: {buttonID: me.id}
makes your webmethod believe that you are sending them an object named buttonID and not as a string as stated in your C# method.
You should change to data: {'buttonID': me.id}
if you want it to be seen as a string and not an object.
I got it to work by making the webmethod static...
[WebMethod]
public static string AddToPrintQueue(string buttonID)
{
...
}
Bit of a bummer because I could no longer reference the input objects directly from the aspx page. All the values needed to be passed through the ajax call.