javascript 如何使用 AJAX 请求 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6057124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I request a url using AJAX
提问by Nikhil Dinesh
I am quite new in this area. I need to find out how to make a request to my solr server using Ajax How can I give a url(my solr server's url) in request Any body know how to deal with this? How can i make a request to the below mentioned url
我在这方面很新。我需要找出如何使用 Ajax 向我的 solr 服务器发出请求 我如何在请求中提供一个 url(我的 solr 服务器的 url) 任何机构都知道如何处理这个问题?我如何向下面提到的 url 发出请求
http://mysite:8080/solr/select/?q=%2A%3A%2A&version=2.2&start=0&rows=100&indent=on
See here: Corrected the Code Snippet as below
请参阅此处:更正代码片段如下
function getProductIds() {
var xmlhttp;
if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) console.dir(xmlhttp);
else alert('no response');
var ajaxURL = "http://localhost:8080/solr/select/?q=*:*&version=2.2&start=0&rows=100&indent=on";
xmlhttp.open("GET", ajaxURL, true);
xmlhttp.send();
}
This is my code, it always showing "no response"
Thanks.
这是我的代码,它总是显示“无响应”
谢谢。
采纳答案by Nikhil Dinesh
$.ajax({
url: "url path",
context: document.body
}).done(function(data) {
alert(data);
});
This one also will work.
这个也能用。
回答by Deeptechtons
You will have to prepare the URL before sending in the request first get the URl using javascript and then encode it to ajax format like below
您必须在发送请求之前准备 URL,首先使用 javascript 获取 URl,然后将其编码为 ajax 格式,如下所示
var URL = location.href;
var ajaxURL = encodeURIComponent(URL);
xmlhttp.open("GET",ajaxURL,true);
after reading your question clearly it seemed it is a static URL hence you can do below
清楚地阅读您的问题后,它似乎是一个静态 URL,因此您可以在下面执行
var URL = "http://localhost:8080/blah blah blah";
xmlhttp.open("GET",URL,true);
Are you sure it is Get request. because get requests are most of the time cached. also log the response object into Firebug console and inspect the object to know more. Since you get no response that means the server did not send you anything for the request you made.
你确定它是 Get 请求。因为获取请求大部分时间都是缓存的。还将响应对象记录到 Firebug 控制台并检查对象以了解更多信息。由于您没有收到任何响应,这意味着服务器没有针对您提出的请求向您发送任何内容。
回答by worldsayshi
I'm just now working on XMLHttpRequests to solr as well and I was stuck with what seems like an identical problem. I too am quite new at this. However, the problem for me was that of same origin policy. Firefox seems to give very little feedback when this problem occurs. Chrome at least give you a error message (most of the time?).
我刚刚也在处理 XMLHttpRequests to solr 并且我被困在了一个看似相同的问题上。我对此也很陌生。但是,对我来说问题是同源政策。发生此问题时,Firefox 似乎很少提供反馈。Chrome 至少会给你一条错误信息(大部分时间?)。
In Chrome you can get around this, but only for development purposes, by starting it with the '--disable-web-security'command line option.
在 Chrome 中,您可以通过使用“--disable-web-security”命令行选项启动来解决此问题,但仅限于开发目的。
I'm yet to find a good workaround for this problem for Solr. In general you avoid the restriction by only using requests with relative paths, but that doesn't seem possible when doing a request to another port.
我还没有为 Solr 找到解决这个问题的好方法。通常,您仅通过使用具有相对路径的请求来避免限制,但在向另一个端口发出请求时似乎不可能。
Ways to circumvent the policy (I haven't had time to study this too much yet)