最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I request a url using AJAX - Stack Overflow

programmeradmin5浏览0评论

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

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.

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

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.

Share Improve this question edited Nov 21, 2012 at 10:57 Nikhil Dinesh asked May 19, 2011 at 10:18 Nikhil DineshNikhil Dinesh 3,4092 gold badges40 silver badges42 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

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

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

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.

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?).

In Chrome you can get around this, but only for development purposes, by starting it with the '--disable-web-security' mand line option.

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.

Ways to circumvent the policy (I haven't had time to study this too much yet)

$.ajax({
   url: "url path",
   context: document.body
   }).done(function(data) {
      alert(data);
});

This one also will work.

发布评论

评论列表(0)

  1. 暂无评论