简体   繁体   English

Ajax调用Java Rest API

[英]Ajax calling a java Rest API

I am having ajax problems that I cannot figure out, and need some help... I am using Spring for my REST api and my ajax calls don't seem to work... I have searched the forums and haven't been able to find an answer: 我遇到无法解决的Ajax问题,需要一些帮助...我将Spring用于我的REST api,而我的Ajax调用似乎不起作用...我已经搜索了论坛,但无法找到答案:

My java Spring api is as follows: 我的Java Spring API如下:

@Controller
@RequestMapping("api")
public class RecentRestController {

    RecentService recentService;

    @Autowired
    public void PersonRestController(RecentService recentService) {
        this.recentService = recentService;
    }

    /**
     * Add recent lake, then get recently viewed lakes and users ordered by timestamp
     * @param handle
     * @return
     */
    @RequestMapping(value = "recent/weather/{auser}/{temp}/{windspeed}/{winddeg}/{laketag}", method = RequestMethod.GET)
    @ResponseBody
    public RecentlyViewedList getRecentlyViewedLakes(@PathVariable String auser, @PathVariable Integer temp, 
            @PathVariable Integer windspeed, @PathVariable Integer winddeg, @PathVariable String laketag) {

        RecentlyViewedList rvl = recentService.getRecentlyViewedWeather(auser, temp, windspeed, winddeg, laketag);
        return rvl;
    }

When I use ajax to call this Java REST from ajax it doesn't seem to work. 当我使用ajax从ajax调用此Java REST时,它似乎不起作用。 My ajax call looks as follows from html/php: 我的ajax调用从html / php看起来如下:

new $Ajax.Request('http://localhost:8080/server/api/weahter/lake/' + agruments.auser + '/' + arguments.windspeed +'/' + arguments.winddeg + '/' + arguments.laketag, {
      type : "GET",
       //:url : recenturl,
       //cache : false,
       async : false,
       crossDomain: true,
       dataType : 'jsonp',
       //data: arguments,
       success : function(recent) {


           alert("SUCESS");
            var i=0;

            var lakecount = recent.lake.length;
            var usercount = recent.user.length;

            alert("lakecount:" + lakecount);
            alert("usercount:" + usercount);

       },
       error : function(XMLHttpRequest, textStatus, errorThrown) {
          alert("An error has occurred making the request: " + errorThrown);
       }, 
    });

It never seems to work. 它似乎从未奏效。 It never calls my REST api correct.. What am I doing incorrectly? 它永远都不会正确地调用我的REST API。

Something is wrong with how I calling my REST service.. 我调用REST服务的方式出了点问题。

Any help is greatly appreciated.. 任何帮助是极大的赞赏..

Thanks in advance. 提前致谢。

As you are checking for a @GET request, the most obvious thing to do is to try to hit the API directly from a browser ( type in your URL field 在检查@GET请求时,最明显的事情是尝试直接从浏览器访问API(在URL字段中键入

http://localhost:8080/server/api/weahter/lake/' + agruments.auser + '/' + arguments.windspeed +'/' + arguments.winddeg + '/' + arguments.laketag 

with the parameters resolved ). 参数已解析)。

Other thing you should be checking is that your context path is 'server' as that is where the URL is pointing. 您应该检查的另一件事是您的上下文路径是“服务器”,即URL指向的位置。

Also you have and spelling error in the first parameter of the URL: 'agruments' instead of 'arguments' 另外,URL的第一个参数中存在拼写错误:“ agruments”而不是“ arguments”

Yeah, the request has recent in it. 是的,请求已在其中。 What I cannot figure out is that if I build it manualy: 我不知道的是,如果我手动构建它:

   url : 'http://localhost:8080/server/api/recent/lake/nightstalker/3/3/3/TXFORK'

it works. 有用。 But when I build it with variables, it does not. 但是,当我使用变量构建它时,它不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM