我编写下面的代码来调用外部Web服务它没有调试器在safari浏览器中工作。因为解决方案我添加了settimeout()函数来延迟xhr.send()函数。下面描述了问题:
我试图在2秒后调用alertFunc但是settimeout()函数没有调用alertFunc。你可以帮我解决它。
我在下面的代码中定义了我的wordpress footer.php文件。
<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>
答案 0 :(得分:1)
包装在一个闭包中:
setTimeout(function(){alertFunc(xhr);}, 25000);
答案 1 :(得分:0)
我通过在以下行中进行更改来解决问题
xhr.open(method,url,true);
替换&#34; true&#34;用&#34;假&#34;值
xhr.open(method,url,false);