Ajax调用在我的脚本中不起作用。但是在邮递员工作

时间:2018-10-04 06:38:22

标签: javascript rest

This is my Postman data

以下是我访问该服务的代码。其余的api是第一个。

$('#LoginOrigin').click(function () {
        jQuery.support.cors = true;
        debugger;
        var mobilenumber = document.getElementById("txtmobileNumber").value;
        var pin = document.getElementById("txtPIN").value;
        var data0 = {mobile_number: mobilenumber, pin: pin};
        var json = JSON.stringify(data0);
        $.ajaxSetup({
            error: function (xhr) {

                document.getElementById("successLable").innerHTML = "";
                //document.getElementById("errorLable").innerHTML = "Error received as " + xhr.status + " Please Contact Admin ";
                document.getElementById("errorLable").innerHTML = "Authentication Failed-" + xhr.status;

            }
        });
        $.ajax(
                {
                    type: "POST",
                    data: json,
                    dataType: "text",
                    contentType: "application/json; charset=utf-8",
                    url: "http://" + window.location.hostname + ":8080/DigitalWallet/login",
                    success: function (data, textStatus, xhr) {
                        document.getElementById("errorLable").innerHTML = "";
                       alert(JSON.stringify(data));
//                        if (typeof (Storage) !== "undefined") {
//                             document.getElementById("successLable").innerHTML = "Authentication Success.. " + data;
//                             localStorage.setItem("userId",data);
//                             window.location.replace("http://stackoverflow.com");
//                        } else {
//                            alert("Please update the browser. The browser is not supported for the web storage")
//                        }
                    }
                });

但是当我调用API时,它不起作用。我在API方法的开头有一个调试器点。但这在我打电话时没有实现。请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

$(document).ready(function(){
$('#LoginOrigin').click(function() {
  jQuery.support.cors = true;
  debugger;
  var mobilenumber = document.getElementById("txtmobileNumber").value;
  var pin = document.getElementById("txtPIN").value;
  var data0 = {
    mobile_number: mobilenumber,
    pin: pin
  };
  var json = JSON.stringify(data0);

  $.ajax({
    type: "POST",
    data: json,
    dataType: "text",
    contentType: "application/json; charset=utf-8",
    url: "http://" + window.location.hostname + ":8080/DigitalWallet/login",
    success: function(data, textStatus, xhr) {
      document.getElementById("errorLable").innerHTML = "";
      alert(JSON.stringify(data));
    }
  });
}); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Mobile :<input id="txtmobileNumber"/>
Pin: <input id="txtPIN"/>
<button id="LoginOrigin">Login</button>

您似乎错过了大括号的操作。您要呼叫的端点是什么?