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>
2
  • Some browsers will not work with the extra comma here at the end: "FindUsName": findus, Commented Dec 4, 2015 at 15:59
  • You create a global, then another private xhr with var xhr = two places that is probably not the best practice here. Commented Dec 4, 2015 at 16:01

2 Answers 2

1

wrap in a closure:

setTimeout(function(){alertFunc(xhr);}, 25000);
Sign up to request clarification or add additional context in comments.

3 Comments

I tried but still not working.actually i define my script in wordpress(footer.php).should be issue with wordpress??
I fixed a minor issue added a ; however I would suggest you look at createing a promise: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 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.
Scratch the Promise if you need IE (Internet Explorer) BTW and use jQuery or look at stackoverflow.com/questions/27835687/… or stackoverflow.com/questions/23772801/… or search for "promise JavaScript"
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.