admin管理员组

文章数量:1426229

I am getting a very strange error while the response is correct and I can see the JSON content:

here is the request:

$.ajax({
                    type: "GET",
                    url: urlTwitter,
                    contentType: "application/json",
                    dataType: "jsonp",
                    async: false,
                    success: function (resp, status, xhr) {
                       $("#message").html("STATUS: " + xhr.status + " " + xhr.statusText + "\n" + resp);
                       $("#message").hide();
                       $.each(resp, function() {
                            $.each(this, function(i, tweet) {
                                arrayTweets.push(tweet);
                            });

                        });

                        displayTweets();
                    },
                    error: function(resp, status, xhr){
                        $("#message").html("ERROR: " + xhr.status + " " + xhr.statusText + "\n" + resp.e);
                        $("#message").show();
                    }
                });

Here is the conent in the response:

{"tweet":[{"text":"RT @OSCARHAROTASEND: MUYYY CONTENTO Y MUYYY ORGULLOSO del trabajo d mi hermanito @pablonieto22 en la presentación d su TEAM @CalvoTeam anoc…","user":"Jemurillove"}]}

Anyone has a clue, I've been fighting with this half day.

Appreciate any help.

Thanks,

EDIT:

this is my resource, if it can help. And the Tweet object is annotated with @XmlRootElement:

@GET
    @XmlElement(name = "tweet")
    @Path("/retrieveTweets")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Tweet> retrieve(@QueryParam("lat") Double Latitude, @QueryParam("lon") Double Longitude, @QueryParam("rad") Double Radius, @QueryParam("from") String From, @QueryParam("to") String To) {
        //List<Status> tweets = null;
        List<Tweet> lTweets = new ArrayList<Tweet>();
        boolean status = false;

        Twitter twitter = new TwitterFactory().getInstance();

        AccessToken accessToken = new AccessToken(TwitterInterface.ACCESS_TOKEN, TwitterInterface.ACCESS_TOKEN_SECRET);
        twitter.setOAuthConsumer(TwitterInterface.CONSUMER_KEY, TwitterInterface.CONSUMER_SECRET);
        twitter.setOAuthAccessToken(accessToken);

        try {
            Query query = new Query("");
            GeoLocation geo =  new GeoLocation(Latitude, Longitude);
            query.setGeoCode(geo, Radius/1000, Query.KILOMETERS);
            query.setCount(100);
            query.setSince(From);
            query.setUntil(To);
            QueryResult result;
            result = twitter.search(query);
            List<Status>tweets = result.getTweets();
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText() + " - " + tweet.getCreatedAt());
                Tweet t = new Tweet();
                t.setUser(tweet.getUser().getScreenName());
                t.setText(tweet.getText());
                lTweets.add(t);
            }
        }
        catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to search tweets: " + te.getMessage());
            System.exit(-1);
        }

        return lTweets;

I am getting a very strange error while the response is correct and I can see the JSON content:

here is the request:

$.ajax({
                    type: "GET",
                    url: urlTwitter,
                    contentType: "application/json",
                    dataType: "jsonp",
                    async: false,
                    success: function (resp, status, xhr) {
                       $("#message").html("STATUS: " + xhr.status + " " + xhr.statusText + "\n" + resp);
                       $("#message").hide();
                       $.each(resp, function() {
                            $.each(this, function(i, tweet) {
                                arrayTweets.push(tweet);
                            });

                        });

                        displayTweets();
                    },
                    error: function(resp, status, xhr){
                        $("#message").html("ERROR: " + xhr.status + " " + xhr.statusText + "\n" + resp.e);
                        $("#message").show();
                    }
                });

Here is the conent in the response:

{"tweet":[{"text":"RT @OSCARHAROTASEND: MUYYY CONTENTO Y MUYYY ORGULLOSO del trabajo d mi hermanito @pablonieto22 en la presentación d su TEAM @CalvoTeam anoc…","user":"Jemurillove"}]}

Anyone has a clue, I've been fighting with this half day.

Appreciate any help.

Thanks,

EDIT:

this is my resource, if it can help. And the Tweet object is annotated with @XmlRootElement:

@GET
    @XmlElement(name = "tweet")
    @Path("/retrieveTweets")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Tweet> retrieve(@QueryParam("lat") Double Latitude, @QueryParam("lon") Double Longitude, @QueryParam("rad") Double Radius, @QueryParam("from") String From, @QueryParam("to") String To) {
        //List<Status> tweets = null;
        List<Tweet> lTweets = new ArrayList<Tweet>();
        boolean status = false;

        Twitter twitter = new TwitterFactory().getInstance();

        AccessToken accessToken = new AccessToken(TwitterInterface.ACCESS_TOKEN, TwitterInterface.ACCESS_TOKEN_SECRET);
        twitter.setOAuthConsumer(TwitterInterface.CONSUMER_KEY, TwitterInterface.CONSUMER_SECRET);
        twitter.setOAuthAccessToken(accessToken);

        try {
            Query query = new Query("");
            GeoLocation geo =  new GeoLocation(Latitude, Longitude);
            query.setGeoCode(geo, Radius/1000, Query.KILOMETERS);
            query.setCount(100);
            query.setSince(From);
            query.setUntil(To);
            QueryResult result;
            result = twitter.search(query);
            List<Status>tweets = result.getTweets();
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText() + " - " + tweet.getCreatedAt());
                Tweet t = new Tweet();
                t.setUser(tweet.getUser().getScreenName());
                t.setText(tweet.getText());
                lTweets.add(t);
            }
        }
        catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to search tweets: " + te.getMessage());
            System.exit(-1);
        }

        return lTweets;
Share Improve this question edited Mar 8, 2014 at 10:05 mzereba asked Mar 8, 2014 at 9:50 mzerebamzereba 2,5175 gold badges27 silver badges42 bronze badges 5
  • are you using <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> in your page? – anurupr Commented Mar 8, 2014 at 9:56
  • @anurupr no I am not. – mzereba Commented Mar 8, 2014 at 9:59
  • Response you posted clearly means, that request does not runs as JSONP, because we don't see data wrapped in callback. I see you assigned async: false, but JSONP request cannot be synchronous. Try to remove this line and check response. It should be like callback323123123123({ /* data here */ }) – Tommi Commented Mar 8, 2014 at 10:05
  • After edit I see you use your own proxy, not direct twitter API call. In this case you need to make JSONP-patible response. See @knolleary answer. – Tommi Commented Mar 8, 2014 at 10:09
  • @Tommi removing async gives the same. – mzereba Commented Mar 8, 2014 at 10:14
Add a ment  | 

1 Answer 1

Reset to default 3

You are requesting a jsonp response, but the response is plain JSON.

You can either change the request type to 'JSON' - if the request doesn't fall foul of cross-domain restrictions, or change whatever is generating the response to wrap the response as JSONP.

For more information on JSONP, have a look at the jQuery docs

In summary, jQuery automatically adds a callback=? parameter to the url it requests. Your server-side code needs to use the value of that parameter as the name of a javascript function your response should invoke with the JSON passed as an argument.

For example, if callback=helloWorld, your response should be:

helloWorld({"tweet": ... });

本文标签: javascriptSyntaxError missingbefore statement in JSONStack Overflow