问题描述
我有一个简单的AJAX/JavaScript函数,该函数会发出GET请求,并在成功的情况下根据URL将页面加载到当前页面中:
I have a simple AJAX/JavaScript function which makes a GET request and loads a page into the current page based on a URL if successful:
function loadDoc(myUrl) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("toolOptions").innerHTML =
this.responseText;
}
};
xhttp.open("GET", myUrl, true);
xhttp.send();
}
稍后我将在同一文档中调用此函数:
And I'm calling the function later on in the same document with this:
< li onclick ="loadDoc(/myUrl/toTheLocalSever)">将数据集添加到视图</li>
(URL是由Scala Play的反向路由生成的,但这正是它在最终源中呈现的方式-因此,我认为Play不是这里的问题).
(the URL is generated by Scala Play's reverse routing, but this is exactly how it renders in the final source - so I don't think Play is the issue here).
但是,每当执行此操作时,我都会在控制台中得到它:
But, whenever I do this, I get this in my console:
SyntaxError:正则表达式标志d
我认为这是引号的问题,所以我在原始函数 loadDoc(myUrl)
中做到了:
I thought it was an issue with the quotes, so I did this in the original function loadDoc(myUrl)
:
var wrappedUrl = "'"+myUrl+"'"
xhttp.open("GET", wrappedUrl, true);
但是那仍然给我同样的错误.我究竟做错了什么?谢谢!
But that still gives me the same error. What am I doing wrong? Thanks!
推荐答案
是的.
您正在尝试编写不带任何字符串的文字.
You are trying to write a string literal without any.
onclick="loadDoc("/myUrl/toTheLocalSever")">
var wrappedUrl = "'"+myUrl+"'"
您无法从引发错误后运行的代码中修复语法错误.
You can't fix a syntax error from code that runs after the error has been thrown.
这篇关于如何将带有/的URL传递给JavaScript对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!