Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果从AJAX接收到url,则Javascript对象无法访问该url_Javascript_Jquery_Ajax_Wikitude - Fatal编程技术网

如果从AJAX接收到url,则Javascript对象无法访问该url

如果从AJAX接收到url,则Javascript对象无法访问该url,javascript,jquery,ajax,wikitude,Javascript,Jquery,Ajax,Wikitude,如果url是手动分配的,例如imgUrl=”http://image.jpg它工作得很好,但是如果它在函数“getRemote”中使用AJAX从后端接收,它就不会。如果请求是异步的,它甚至不在这个块中,但是如果它是同步的,它会被接收,但是AR对象不能使用它。你知道为什么吗 var imgUrl = getRemote(); this.img = new AR.ImageResource(imgUrl); this.imgOverlay = new AR.ImageDrawable(this.im

如果url是手动分配的,例如imgUrl=”http://image.jpg它工作得很好,但是如果它在函数“getRemote”中使用AJAX从后端接收,它就不会。如果请求是异步的,它甚至不在这个块中,但是如果它是同步的,它会被接收,但是AR对象不能使用它。你知道为什么吗

var imgUrl = getRemote();
this.img = new AR.ImageResource(imgUrl);
this.imgOverlay = new AR.ImageDrawable(this.img, 0.5, {
    offsetX: 0,
    offsetY: 0,
});
getRemote功能:

function getRemote() {
return $.ajax({
    type: "GET",
    url: "http://someurl.php",
    async: true
}).responseText;

}

除非您可以等待响应,否则方法无法返回ajax响应,相反,您应该将代码包装在函数中,并将其用作回调函数,在ajax完成后执行:

var callOnRemote = function(imgUrl){
   this.img = new AR.ImageResource(imgUrl);
   this.imgOverlay = new AR.ImageDrawable(this.img, 0.5, {
       offsetX: 0,
       offsetY: 0,
   });
};

getRemote(callOnRemote);
带有jquery的getRemote函数如下所示:

$.ajax({
  url: "/test.html"
}).success(callOnRemote);

最好甚至显示getRemote()代码,因为感觉错误可能在该函数中。。我们可以看到
getRemote()
方法的结果/代码吗?这可能是问题的根源,也许