Alert via jQuery not working as planned

Asked

Viewed 72 times

0

I need a help!

I am developing a web project, and in it I call jQuery a WCF SOAP test, very simple, on a button, and returns an Alert with a value. It’s working, but not the way I want it to. In the first click appears nothing, in the second appears 2 Alerts, in 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);

        }
      });
    });
  });
}
  • You’re looking with .find within the return of Ajax something with that name GetDataResponse... if you do not, you will not give any Alert, but if you do, you will give Alerts according to the amount you find.

  • He looks for the SOAP envelope I put in the jhRequest variable. In case it always increases one Lert every click, would know me did something wrong or if you have another alternative?

1 answer

0


The increase in the number of calls from alert happens because you are linked a new event every click on the element #btnWCFWSHttp.

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

$("#btnWCFWSHttp").off().click(function () {
    //your code
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.