Ajax回调将所需的URL附加到现有的URL [英] Ajax callback appending desired url to existing url
问题描述
我试图通过ajax调用Drupal7.我遇到的问题是,我要用于进行回调的URL附加到用户正在查看的当前页面上.我不确定为什么会这样,并且想知道是否有人可以为我指出我的错误.这是我用来拨打电话的javascript代码:
I am trying to make a ajax call back to a Drupal 7. The problem I am encountering is that the url I want to use to make the callback is appended to the current page the user is viewing. I am not sure why this is happening and am wondering if some can point out my error for me. Here is the javascript code I am using to make the call:
(function($) {
function todaysHours(context) {
var callbackFunction = window.location.host +'/' + Drupal.settings.library_hours.callbackFunction,
content = $("#todays-hours").find(".block");
nIntervId = setInterval(checkTime, 300000);
function checkTime() {
request = $.ajax({
url: callbackFunction,
dataType: "json",
type: "GET"
});
request.done(function( result ) {
content.text(result[0].data);
})
}
}
Drupal.behaviors.library_hours = {
attach: function(context) {
todaysHours(context);
}
}
})(jQuery);
我希望使用的网址是 http://mydomain.com/ajax/get-time,但是ajax调用中实际使用的是 http://即使callbackfunction变量设置为mydomain.com/ajax/get-time,mydomain.com/current-page/mydomain.com/ajax/get-time .
The url I expect to use is http://mydomain.com/ajax/get-time but what is actually being used in the ajax call is http://mydomain.com/current-page/mydomain.com/ajax/get-time even though the callbackfunction variable is set to mydomain.com/ajax/get-time.
为什么会发生这种情况,我该如何解决?谢谢.
Why is this happening and how do I fix it? Thanks.
推荐答案
问题: 网址中未定义协议
Problem: Protocol is not defined in the url
解决方案: 更新代码中的以下部分
Solution: update the following part in the code
(function($) {
function todaysHours(context) {
var callbackFunction = '//'+window.location.host +'/' + Drupal.settings.library_hours.callbackFunction,
// rest code
})(jQuery);
这篇关于Ajax回调将所需的URL附加到现有的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!