0

I am trying to do a GET request to solr search engine using $.ajax() from jQuery. This is the code I am using for doing an ajax call:

$.ajax({
            url : 'http://xxx.xxx.xxx.xxx:port#/solr/mycore/select?indent=on&q=myparam:value&wt=json',
            type: "GET",
            dataType: "jsonp",
            jsonp : "callback",
            success: function(data) {
                console.log("Success", data);
            },
            error: function(data) { alert("Error"); }
        });

So I am getting a valid json object in the response but somehow the browser throws an Uncaught Syntax Error. The actual error is:

Uncaught SyntaxError: Unexpected token :

select?indent=on&q=myparam:value&wt=json&callback=…....somevalue...

The tricky part is that the response header is text/plain when I checked in the browser. How can I solve this? Please help me...

Community
  • 1
  • 1
Horcrux
  • 5
  • 7
  • It seems the server is not sending back valid JSONP. JSON is not the same as JSONP. I guess this is a duplicate of [json Uncaught SyntaxError: Unexpected token :](http://stackoverflow.com/q/7936610/218196) – Felix Kling Jun 08 '16 at 04:32
  • Check if any of your single/double quotes are messed up in the HTML tags. – Vinay Jun 08 '16 at 04:35

2 Answers2

0

Colons require encoding in query strings.

Your url should look like this:

http://xxx.xxx.xxx.xxx:port#/solr/mycore/select?indent=on&q=myparam%3Avalue&wt=json

If you're generating the query string dynamically, use encodeURIComponent() to correctly encode special characters.

Community
  • 1
  • 1
hotforfeature
  • 2,558
  • 1
  • 16
  • 24
0

I got this solved. Actually I had to use jsonpCallback:"mysuccesscallbackfunction" in the ajax call and json.wrf=mysuccesscallbackfunction in the URL. It looks like this now:

$.ajax({
url : 'http://xxx.xxx.xxx.xxx:port#/solr/mycore/select?indent=on&q=myparam:value&wt=json&json.wrf=mysuccesscallbackfunction',
        type: "GET",
        dataType: "jsonp",
        jsonp : "callback",
        jsonpCallback:"mysuccesscallbackfunction",
        success: function(data) {
            console.log("Success", data);
        },
        error: function(data) { alert("Error"); }
    });

and my mysuccesscallbackfunction is:

function mysuccesscallbackfunction(resp) {
        console.log('inside mysuccesscallbackfunction: ', resp );
    }

Now, first it executes whatever is inside mysuccesscallbackfunction and then goes to default success callback. I found it somewhere on web. I would still like to know why it worked now.

Horcrux
  • 5
  • 7