Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.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
Javascript js";脚本“;XMLHttpRequest调用前的文件夹名称_Javascript_Jquery - Fatal编程技术网

Javascript js";脚本“;XMLHttpRequest调用前的文件夹名称

Javascript js";脚本“;XMLHttpRequest调用前的文件夹名称,javascript,jquery,Javascript,Jquery,我在Web服务器的“scripts”文件夹中有一个名为myscripts.js的javascript文件。可通过以下方式访问: 在myscripts.js中有一个javascript函数,它对我的网站的somemethod.html进行XMLHttpRequest调用。以下是呼叫代码: xmlhttp.open("GET","somemethod.html",false); 99%的时间一切正常。但我发现一些浏览器在调用前添加了“脚本/”。结果是这样一个调用: http://www.exam

我在Web服务器的“scripts”文件夹中有一个名为myscripts.js的javascript文件。可通过以下方式访问:

在myscripts.js中有一个javascript函数,它对我的网站的somemethod.html进行XMLHttpRequest调用。以下是呼叫代码:

xmlhttp.open("GET","somemethod.html",false);
99%的时间一切正常。但我发现一些浏览器在调用前添加了“脚本/”。结果是这样一个调用:

http://www.example.com/scripts/somemethod.html
什么时候应该是这样:

http://www.example.com/somemethod.html
这是一个定制的Web服务器(即,我基本上处理所有请求)

  • 我的Web服务器应该能够处理这个问题吗?或者这只是一个我不应该担心的不可靠的浏览器
  • 我不应该在javascript中使用“相对”路径吗?而是在java脚本中使用绝对调用?e、 g:它的编码应该是这样的,而不是“somemethod.html”:

    http://www.example.com/scripts/somemethod.html
    
    open(“GET”,“”,false)

  • 在JavaScript中使用相对路径是绝对好的(而且绝大多数是实践标准),只需注意它们与的相对关系:包含JavaScript的文档(不是JavaScript文件)。您似乎对此很清楚,但只是强调了一下

    我从来没有见过浏览器会弄错这一点。您看到的请求可能来自一个写得不好的网络爬虫,它查看JavaScript的源代码,而不是做一些智能的事情,比如找出它在哪里/如何运行

    不过,为了清楚起见,关于相对的事情(对潜伏者来说比对你来说更多):

    鉴于这种结构:

    foo.html index.html js/ script.js
    …然后使用该脚本文件中的代码执行XHR调用,调用将在功能正常的浏览器上相对于
    index.html
    ,而不是
    script.js

    我从不使用相对请求,我构建了自己的url,通过js代码构建url,并使用“toString”方法传递url,以准确地给出我需要的url

    另外,尽量不要再使用同步XHR调用,理想情况下应该使用异步和回调,这很痛苦,但这是最好的

    client . open(method, url [, async = true [, username = null [, password = null]]])
    
        Sets the request method, request URL, and synchronous flag.
    
        Throws a "SyntaxError" exception if either method is not a valid HTTP method or url cannot be parsed.
    
        Throws a "SecurityError" exception if method is a case-insensitive match for `CONNECT`, `TRACE` or `TRACK`.
    
        Throws an "InvalidAccessError" exception if async is false, the JavaScript global environment is a document environment, and either the timeout attribute is not zero, the withCredentials attribute is true, or the responseType attribute is not the empty string. 
    

    来源:

    “一些浏览器”-什么浏览器?你确定他们是浏览器而不是垃圾机器人吗?很好。我将把用户代理添加到我的日志中。正如@Quentin所说的,可能是某种爬虫。我将记录用户代理/ip地址以了解更多信息。