简体   繁体   English

Ajax调用总是返回错误

[英]Ajax call always returning error

I'm having trouble with calling a WebMethod with Jquery. 我在用Jquery调用WebMethod时遇到麻烦。

function runQuery(e) {
  var search = $(e).val();
  var csKind;

  if ($('#rbLP').is(':checked'))
    csKind = 1;
  else
    csKind = 0;

  var params = {
    url: 'addEditProduct.ascx/AutoComplete_Press',
    method: 'post',
    contentType: 'application/json',
    data: JSON.stringify(search),
    dataType: 'json',
    success: function(data) {
      alert(1);
    },
    error: function(data) {
      alert(2);
    }
  };

  $.ajax(params);
}
[WebMethod]
public static void AutoComplete_Press(string searchClause, int csKind)
{
  int searchType = 0; //ЕГН

  Regex regex = new Regex("^[0-9]+$");
  if (!regex.IsMatch(searchClause))
    searchType = 1;

  string clients = laboratory.getClients2(searchType, searchClause, csKind);
}

How can I diagnose the problem, I've never used ajax before and I'm at a loss. 我如何诊断问题,我以前从未使用过Ajax,现在很茫然。

The problem I can see here is your passing arguments: 我在这里看到的问题是您传递的参数:

data: JSON.stringify(search),

you are missing csKind, maybe change this line to 您缺少csKind,也许将此行更改为

data: "{searchClause: '" + search + "',csKind: '" + csKind + "'}",

And change your method to : 并将您的方法更改为:

public static void AutoComplete_Press(string searchClause, string csKind)

The url seems to be wrong if you ask me. 如果您问我,URL似乎是错误的。 Open up your console in the browser and see what it says, it will be throwing cant connect/connection refused error. 在浏览器中打开控制台,查看其内容,它将抛出无法连接/连接被拒绝的错误。 Also open network in the browser and you can check what http response you are getting. 另外,在浏览器中打开网络,然后您可以查看所收到的http响应。 This will help u start up diagnosing the problem. 这将有助于您开始诊断问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM