[英]My Ajax function is making double request
This is my function to check if the coupon code is valid or not. 这是我检查优惠券代码是否有效的功能。
function checkCoupon(str){
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
alert('success');
}
}
xmlhttp.open("GET","https://www.example.com/inc/checkCoupon?id=45&coupon="+str,true);
xmlhttp.send();
}
I am using <input onchange="checkCoupon(this.value)" placeholder="Coupon">
to call this. 我正在使用
<input onchange="checkCoupon(this.value)" placeholder="Coupon">
来调用它。
The problem is, It is making two different calls. 问题是,它正在拨打两个不同的电话。 First
https://
version of the page then it was immediately canceled after that it makes another call on http://
version which is rejected because of mixed-content . 页面的第一个
https://
版本,然后立即被取消,之后再次调用http://
版本,但由于内容混合而被拒绝。
I have also checked it I have more than one function to do the same work but I have an only single function on this page. 我也检查过它有多个功能可以完成相同的工作,但是此页面上只有一个功能。
Here is the snap of network tab in Inspect Element >> https://imgur.com/a/fQnXuQ1 这是检查元素>> https://imgur.com/a/fQnXuQ1中“网络”选项卡的快照
Above is https://
and below is http://
. 上面是
https://
,下面是http://
。
Thanks for the help. 谢谢您的帮助。
It seems like you have some active http content when you're making the https request. 发出https请求时,似乎有一些活动的http内容。 For more information, see here .
有关更多信息,请参见此处 。
A question though, why don't you just force the entire site's communication to be via https in the first place? 但是,有一个问题,为什么不首先只是强制整个网站的通信通过https? This can be done in the htaccess file.
这可以在htaccess文件中完成。 Example:
例:
#Rewrite everything to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
There was indeed something wrong in the .htaccess
file. .htaccess
文件中确实存在问题。 This type of problem actually happens because of .htaccess
. 实际上,由于
.htaccess
导致了这种类型的问题。 I will answer half of the question because I don't know the answer to the other half. 我将回答一半的问题,因为我不知道另一半的答案。
This was my .htaccess
code for redirection to www then https:// 这是我的
.htaccess
代码,用于重定向到www然后是https://
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is actually alright. 其实没关系。 What I have missed is another code below it.
我错过的是它下面的另一个代码。 This below code is used to force the trailing slashes in URL.
下面的代码用于强制在URL中使用尾部斜杠。
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]
As you can see, what I have missed is the last row in this code. 如您所见,我错过的是此代码的最后一行。 I have forced trailing slashes to
http://www.exaple.com
which is actually supposed to be https://www.exaple.com
. 我已将斜杠强行插入到
http://www.exaple.com
,而实际上应该是https://www.exaple.com
I have missed http://
here which is actually supposedly be https://
. 我错过了
http://
,实际上应该是https://
。
Another half answer, which I don't know is: It is still making the double request for every AJAX query call. 我不知道的另一半答案是:它仍然对每个AJAX查询调用都提出双重要求。 I have a guess about it, It must be due to the
non-www
to www
redirection then http://
to https://
redirection. 我对此有一个猜测,这一定是由于
non-www
到www
重定向,然后是http://
到https://
重定向。 It is still my guess though. 虽然这仍然是我的猜测。
Thanks to @Martin, I at least got the idea about my fault. 多亏@Martin,我至少知道了我的错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.