-1

I want to make a cross domain ajax request using json`

$(function() {
    $(".x25").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "http://localhost:8983/solr/select",
                processData: false,
                crossDomain: true,
                contentType: "application/json",
                jsonp: false,
                data: {
                    q: "name:" + request.term + "*",
                    wt: "xslt",
                    tr: "solr2json.xsl"
                },
                dataType: "jsonp",
                success: function() {
                    alert("Success");
                },
                error: function() {
                    alert("failure");
                }
            });
            return false;
        }
    });
});

This is my code, i am keep trying with json and jsonp but did not get succeed and i dont have any control on remote server. Any help.

Siva
  • 1,938
  • 1
  • 17
  • 36
  • 1
    What is the meaning of using `jsonp: false` & `dataType : "jsonp"` together, in your code above?? – palaѕн Jun 24 '13 at 06:50
  • http://stackoverflow.com/questions/2681466/jsonp-with-jquery – Muntasim Jun 24 '13 at 06:58
  • @Palash Ohh.. its my mistake though i remove that it is not working – Siva Jun 24 '13 at 07:02
  • error message? Cross domain shouldn't affect you here, you can retrieve data from anywhere as long as the requested host + port + protocol match the response host + port + protocol – TecHunter Jun 24 '13 at 08:20
  • @user2503916 remove the `crossDomain: true, jsonp:false` – TecHunter Jun 24 '13 at 08:22
  • @TecHunter i am getting SyntaxError: syntax error in my firebug – Siva Jun 24 '13 at 09:18
  • @user2503916 that's because you get a XML response from your server and not a jsonp response. open this in your browser `http://localhost:8983/solr/select?name=test*&wt=xslt&tr=solr2json.xsl` what do you get – TecHunter Jun 24 '13 at 10:05

4 Answers4

1

Found the solution for this, it was my mistake in understanding solr response. As i was requesting it for solr response it does not require any .xslt or xslt2json. I can directly get the response by making ws: json in my ajax request.

 $(function() {
    $(".x25").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "http://localhost:8983/solr/select",
                processData: false,
                crossDomain: true,
                contentType: "application/json",
                jsonp: false,
                data: {
                    q: "name:" + request.term + "*",
                    wt: "json"
                },
                success: function() {
                    alert("Success");
                },
                error: function() {
                    alert("failure");
                }
            });
            return false;
        }
    });
});
Siva
  • 1,938
  • 1
  • 17
  • 36
0

Put this in your JS:

 <script>            
      jQuery.support.cors = true;
      $(document).ready(function(){
          //stuff
      }
 </script>        
Balint Bako
  • 2,500
  • 1
  • 14
  • 13
  • It didn't work. i just added this line just before my function call – Siva Jun 24 '13 at 07:06
  • 1
    I usually add it before the jquery code. (updated the answer) What is your error message by the way_ – Balint Bako Jun 24 '13 at 07:42
  • I am getting below error in my firebug console SyntaxError: syntax error – Siva Jun 24 '13 at 09:16
  • That is the response already, which is invalid and also not JSON, but XML. An invalid XML that is... – Balint Bako Jun 24 '13 at 09:19
  • Can you please elaborate what causes that error and where i am going wrong – Siva Jun 24 '13 at 09:21
  • 1
    The response form the server is probably not a valid xml. As it is a SOLR search server, I assume you don't have much affect on that one. You should check the actual server response with Fiddler or with some tool. Also if you could update the code you are using right now, that would help the community to help you... – Balint Bako Jun 24 '13 at 09:27
  • You might want to read this http://wiki.apache.org/solr/SearchHandler as you can get JSON response too... If you still have issue you should raise another question as it is a SOLR thing from this point and not jquery... – Balint Bako Jun 24 '13 at 09:31
  • Thank you but i am successfully getting the json data from solr when i try from a java file the issue i am facing is integrating it with textbox which is in my ui page – Siva Jun 24 '13 at 09:33
  • 1
    I'm no export of the SOLR REST(?) search, but you might need `wt=json` in the request to get a json response. I don't know if the xml and xslt way is necessary or what that is, it seems easier to request a json.... – Balint Bako Jun 24 '13 at 09:46
  • Yup you are right Banlint. I have been mis guided and i fixed it now and it is working fine Thank you very much for helping me :) – Siva Jun 24 '13 at 10:53
0
$.ajax({
   url: "http://localhost:8983/solr/select",
   dataType: 'jsonp'
    data: {
           q: "name:" + request.term + "*",
           wt: "xslt",
           tr: "solr2json.xsl"
          },
    success: function() {
           alert("Success");
    },
    error: function() {
            alert("failure");
    }
});

Try this out this

pixelbyaj
  • 1,178
  • 1
  • 9
  • 22
0
dataType : 'jsonp',
jsonp : 'json.wrf',