0

I write code below to call external web services it's not worked in safari browser without debugger.so as solution i add the settimeout() function to delay the xhr.send() function.Real problem is below described:
I trying to call alertFunc after 2 second but settimeout() function is not call the alertFunc.Can you help me to solve it. I define below code my wordpress footer.php file.

<script type="text/javascript">
debugger;
var xhr;
      // Create the XHR object.
      function createCORSRequest(method, url) {

           xhr = new XMLHttpRequest();
           if ("withCredentials" in xhr) {
             // XHR for Chrome/Firefox/Opera/Safari.

        xhr.open(method, url, true);


           } else if (typeof XDomainRequest != "undefined") {
             // XDomainRequest for IE.
             xhr = new XDomainRequest();
             xhr.open(method, url);
           } else {
             // CORS not supported.
             xhr = null;
           }
           return xhr;
         }

    function alertFunc(xh) {
        debugger;
           var xh1=xh;
           xh1.send();
       alert("Thank you for contacting us!");

    }
        function saveContactData() {
            var name = document.getElementById('name').value;
            var petname = document.getElementById('petname').value;
            var weight = document.getElementById('weight').value;
             var breed = document.getElementById('breed').value;
            var phone = document.getElementById('phone').value;
            var email = document.getElementById('email').value;
            var findus = document.getElementById('findus').value;
            var location = document.getElementById('location').value;
            var comments = document.getElementById('comments').value;



            var item = {
                "PetInquiryId": -1,
                "ClientName": name,
        "Weight":weight,
                "Breed":breed,
                "PhoneNumber": phone,
                "PetName": petname,
                "Email": email,
                "Comments": comments,
                "Location": location,
                "FindUsName": findus,

            };
        debugger;
             var url = "http://sitename/api/Controllername/functionnme?item=" + JSON.stringify(item);
            var xhr = createCORSRequest("POST",url);   

        //xhr.send();
         setTimeout(alertFunc(xhr), 25000);


       document.getElementById("myForm").reset();


        }


    </script>
piyush
  • 293
  • 7
  • 21

2 Answers2

1

wrap in a closure:

setTimeout(function(){alertFunc(xhr);}, 25000);
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
  • I tried but still not working.actually i define my script in wordpress(footer.php).should be issue with wordpress?? – piyush Dec 04 '15 at 06:40
  • I fixed a minor issue added a `;` however I would suggest you look at createing a promise: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise as it would seem that prior to your call you reset the form? Did you wish that? Another option is to add jQuery and use it's ajax which returns a promise as well as normalizing the cross browser issues. – Mark Schultheiss Dec 04 '15 at 15:58
  • Scratch the Promise if you need IE (Internet Explorer) BTW and use jQuery or look at http://stackoverflow.com/questions/27835687/is-there-a-way-to-implement-promises-in-ie9 or http://stackoverflow.com/questions/23772801/basic-javascript-promise-implementation-attempt/ or search for "promise JavaScript" – Mark Schultheiss Dec 04 '15 at 16:16
0

I solved issue by make changes in following lines

xhr.open(method, url, true);

replace "true" with "false" value

xhr.open(method, url, false);

piyush
  • 293
  • 7
  • 21