简体   繁体   中英

Ajax call always returning error

I'm having trouble with calling a WebMethod with Jquery.

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.

The problem I can see here is your passing arguments:

data: JSON.stringify(search),

you are missing csKind, maybe change this line to

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. 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. This will help u start up diagnosing the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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