Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 意外令牌>;jquery ajax_C#_Jquery_Asp.net_Ajax - Fatal编程技术网

C# 意外令牌>;jquery ajax

C# 意外令牌>;jquery ajax,c#,jquery,asp.net,ajax,C#,Jquery,Asp.net,Ajax,我正在对一个webmethod进行ajax调用,得到了错误“unexpected Token>”,我不知道为什么会得到这个。。有什么想法吗 function addToQueue(me) { if (validateTimeFrame()) { $.ajax({ method: "POST", url: "GenerateReportModal.aspx/AddToPrintQueue?id=" + $('#patient

我正在对一个webmethod进行ajax调用,得到了错误“unexpected Token>”,我不知道为什么会得到这个。。有什么想法吗

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返回一个字符串,但您已经告诉jQuery需要JSON

$.ajax({
    //...
    dataType: "json",
    //...
});
将其更改为文本(或完全删除),看看这是否解决了您的问题。请参阅的
dataType
属性。

dataType:“json”
,要求您从
webmethod
返回
json
类型!!所以你需要用下面的一行装饰你的
webmethod

[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";
}
除此之外,请尝试在
ajax
调用中指定
contentType
,如下所示:

$.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');
      }
});

我认为WebMethod不接受查询字符串参数。您应该尝试仅通过post方法调用webmethod。

一个快捷方式可能与问题本身有关,也可能只是路上的提示

这一行:
data:{buttonID:me.id}
使您的webmethod相信您正在向他们发送一个名为buttonID的对象,而不是像C#方法中所述的字符串


如果希望将其视为字符串而不是对象,则应更改为
数据:{'buttonID':me.id}

我通过使webmethod为静态来实现它

[WebMethod]
public static string AddToPrintQueue(string buttonID)
{  
...
}

有点不高兴,因为我再也不能直接从aspx页面引用输入对象了。需要通过ajax调用传递的所有值。

您是否将
数据
导入
警报(数据)
?这是因为浏览器不希望您从webmethod返回的结束。您的回答在语法上有问题。@Dhaval-它不会成功,所以我不确定。它正在返回错误函数。@Michael-从web方法返回的响应?它应该是字符串以外的东西吗?我需要使web方法成为静态的,这样它才能工作。不幸的是,我无法再通过添加“runat=server”直接引用aspx页面上所有输入对象的值。我必须通过web方法的参数传递它们,当然,假设您希望客户机上的数据是JSON,而不是字符串。如果您想要一个字符串(正如您的webmethod所示),那么只需从
$.ajax()
调用中取出dataType属性,如我所述。
$.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');
      }
});
[WebMethod]
public static string AddToPrintQueue(string buttonID)
{  
...
}