Wordpress AJAX请求从HTTPS重定向到HTTP

时间:2018-05-08 20:43:07

标签: ajax wordpress http https

我有一个简单的AJAX请求被重定向到HTTP。我已经尝试添加尾随'/'并将URL设为“https”,正如我在类似帖子中看到的那样,但它仍然被拒绝。

jQuery.ajax({ url: "https://"+ host +"/wp-content/plugins/calendarview/views/updateRollOut.php/",
         data: {id: eventID, start: start},
         type: 'GET',
         success: function(output) {
                      document.getElementById(rollOutDetails).innerHTML = output;
                  }
    });

在Google Chrome控制台中,错误显示为:

jquery.js:1 Mixed Content: The page at 
'https://pedalracing.org/newcalendar/' was loaded 
over HTTPS, but requested an insecure XMLHttpRequest endpoint
'http://pedalracing.org/wp-content/plugins/calendarview/views/updateRollOut.php?
id=300770&start=1525905000'.
This request has been blocked; the content must be served over HTTPS.

当我点击Chrome错误中提供的指向端点的链接时,会转到我的网址:

https://pedalracing.org/wp-content/plugins/calendarview/views/updateRollOut.php?id=300770&start=1525905000

我无法弄清楚请求被重定向的位置/原因。我查看了WordPress的301重定向,但没有任何内容会影响此页面。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

查看关于using Ajax in plugins的法典文章。你应该做的是本地化你的JavaScript并包含你想要去的资源的“真实”链接。

使用wp_localize_script,您可以执行以下操作:

wp_localize_script( 'script-name', 'MY_AJAX_OBJECT',
    array( 
        'content_dir' => home_url( 'wp-content/plugins/calendarview/views/updateRollOut.php' )
    )
);

将上述内容放在您的主题(或更好的插件)文件(.php)中,并将其挂钩到用于排队脚本文件的同一个钩子中。理想情况下使用wp_enqueue_scripts操作挂钩。

然后您可以将Ajax请求重写为:

jQuery.ajax(
    {
        url: MY_AJAX_OBJECT.content_dir,
        data: {id: eventID, start: start},
        type: 'GET',
        success: function(output) {
            document.getElementById(rollOutDetails).innerHTML = output;
        }
    }
);
相关问题