Alert via jQuery not working as planned

0

I need a help!

I'm developing a web project, and in it I call jQuery a WCF SOAP test, pretty simple, on a button, and it returns an alert with a value. It's working, but not the way I want it. On the first click nothing appears, the second appears 2 alerts, the third 3, and so on. Could someone check and help me please?

Service call:

teste() {
  jQuery.support.cors = true;
  const jhRequest = "<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">" +
    "<s:Header>" +
    "<a:Action s:mustUnderstand=\"1\">http://tempuri.org/IService1/GetData</a:Action>" +
    "<a:MessageID>urn:uuid:7fdde7b6-64c8-4402-9af1-cc848f15888f</a:MessageID>" +
    "<a:ReplyTo>" +
    "<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>" +
    "</a:ReplyTo>" +
    "<a:To s:mustUnderstand=\"1\">http://localhost:61548/Service1.svc/jh/</a:To>" +
    "</s:Header>" +
    "<s:Body>" +
    "<GetData xmlns=\"http://tempuri.org/\">" +
    "<value>9</value>" +
    "</GetData>" +
    "</s:Body>" +
    "</s:Envelope>";

  $(document).ready(function () {
    $("#btnWCFWSHttp").click(function () {
      $.ajax({
        type: "POST",
        url: "http://localhost:61548/Service1.svc/jh/",
        data: jhRequest,
        timeout: 10000,
        contentType: "application/soap+xml",
        dataType: "xml",
        async: true,
        success: function (data, status, xhr) {
          $(data).find("GetDataResponse").each(function () {
            alert($(this).find("GetDataResult").text());
          });
        },
        error: function (xhr, status, error) {
          alert(error);

        }
      });
    });
  });
}
    
asked by anonymous 06.03.2018 / 15:10

1 answer

0

The increase in the amount of alert calls happens because you are bound to a new event with each click on the #btnWCFWSHttp element.

My suggestion would be to remove events bound to the element before adding a new one, using .off() . For example:

$("#btnWCFWSHttp").off().click(function () {
    //your code
}
    
07.03.2018 / 02:27