jQuery在localhost上向REST发送OPTIONS而不是POST请求

时间:2014-03-07 15:02:36

标签: javascript jquery ajax rest post

我正在尝试通过POST将表单发送到我的REST资源(Java),但我无法,因为我的请求将作为OPTIONS发送。我知道REST资源很好,因为它在使用Poster Firefox进行测试时效果很好。

jQuery / Ajax调用

function loadTwitter(){
            arrayTweets = new Array();
            var urlTwitter = "http://localhost:8081/streamingvideoservice/services/twitter/retrieveTweets";
            $.ajax({
                    type: "POST",
                    url: urlTwitter,
                    contentType: "application/x-www-form-urlencoded",
                    //accept: "application/json",
                    data: $("form#mapForm").serialize(),
                    dataType: "json",
                    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, item) {
                                arrayTweets.push(item);
                            });

                        });

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

REST资源

    @POST
    @Path("/retrieveTweets")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces("application/json")
    public List<Tweet> retrieve(@FormParam("lat") Double Latitude, @FormParam("lon") Double Longitude, @FormParam("rad") Integer Radius, @FormParam("from") String From, @FormParam("to") String To) {

        ArrayList<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, 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;

    }

我正在使用jQuery 1.9.1并在Tomcat 6上托管资源。

感谢任何帮助。

提前致谢。

2 个答案:

答案 0 :(得分:2)

您似乎正在制作跨域Ajax请求。这要求服务器提供Access-Control-Allow-Origin标头,以授予托管包含JS的页面的站点的权限,以便读取数据。

关于请求的某些内容(可能是jQuery添加到Ajax请求的X-Requested-With标头)正在触发preflight request,它在发出主请求之前使用OPTIONS请求向服务器请求权限。

您需要根据CORS规范(上面链接)配置服务器以提供带有合适访问控制头的OPTIONS响应。

答案 1 :(得分:-3)

使用GET解决它并传递URI中的参数。

相关问题