jquery - how to correctly call my rest api from ajax javascript Client -


i have rest api , wanting invoke ajax javascript client.

i make call , nothing happends. mobile browser indicates 'busy'.

no error returned.

this code behind:

[httppost] [route("services/image/validate")] public byte validate(string keycode) {     return 1; } 

this javascript client:

$("#btnvalidate").click(function () {     try {                     var url = "http://my_domain_name/api/services/image/validate";         $.ajax({             type: 'post',             url: url,             data: '{ "keycode" : "' + $("#keycode").val() + '" }',             contenttype: 'application/json; charset=utf-8',             datatype: 'json',             success: function (msg) {                 $('#divstep1').hide();                 $('#divstep2').show();             },             fail: function (a) {                 $("#error").html(objrequest);             }         });     }     catch (ex)     {         $("#error").html(ex);     }     return false; }); 

the catch function throw error when http request fails. in order know wha'ts going wrong in server side code need use error function in ajax request,

you mentioned browser nothing , might happen due

  1. jquery plugin not included in page (check console $ not defined)
  2. add jquery plugin above javascript
  3. if server side code on different domain enable cors

    $("#btnvalidate").click(function () {         var url = "http://my_domain_name/api/services/image/validate";  $.ajax({      type: 'post',      url: url,      data: '{ "keycode" : "' + $("#keycode").val() + '" }',      contenttype: 'application/json; charset=utf-8',      datatype: 'json',      success: function (msg) {          $('#divstep1').hide();          $('#divstep2').show();      },      error: function (e) {          console.log(e.responsetext); //acutal error          console.log(e); //get entire error details      }  }); }); 

Comments