简体   繁体   English

Ajax调用消耗本地主机上的休息服务

[英]Ajax call to consume rest service on local host

Hi I have written a rest service using the Spring framework Below is the code. 嗨,我已经使用Spring框架编写了rest服务。下面是代码。 It returns Json appropriately. 它适当地返回Json。

@ResponseBody
@RequestMapping(value="/showProcessUsage/" ,method=RequestMethod.GET)
public SystemProcessInfo getASingleProcessInfo()
{

    String processName="chrome" //hard coded just for trials;       
    SystemProcessInfo processInfo ;
    processInfo = processInfoService.getASingleProcessUsage(processName);
    return processInfo;
}

In the Html I am trying to make an ajax call, but it is failing below is the call 在HTML中,我正在尝试进行ajax调用,但以下操作失败了

    $.ajax({ 
        type: "GET",
        dataType: "json",
        headers: {
            Accept:"application/json",
            "Access-Control-Allow-Origin": "*"
        },
        url: "/PerformanceMonitor/showProcessUsage/",
        success: function(data){        
            alert("HI");
            alert(data);
           alert("HI");
        }
    });

There a number of code issues where it could fail: 可能会失败的代码有很多:

First, Your rest service endpoint expects a 首先,您的休息服务端点期望

@PathVariable("processName")

which I don't think you are passing in the AJAX call. 我认为您没有通过AJAX调用。

try with the following line 尝试以下行

url: "/PerformanceMonitor/showProcessUsage/xyz-process"

Secondly, your Spring controller method should contain the pathVariable defined in the annotation: 其次,您的Spring控制器方法应包含在注释中定义的pathVariable:

@RequestMapping(value="/showProcessUsage/{processName}"

For debugging: 用于调试:

put an error block in your ajax call. 在ajax调用中放置一个错误块。

error: function(response){ 
            alert(response);
        }

Thee js file for jquery that I included was corrupt .. I tried using the google cdn for jquery and it worked fine for me. 我包含的jquery的js文件已损坏..我尝试将Google cdn用于jquery,它对我来说效果很好。 Thanks for your time :) 谢谢你的时间 :)

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

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