简体   繁体   中英

Ajax call to consume rest service on local host

Hi I have written a rest service using the Spring framework Below is the code. It returns Json appropriately.

@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

    $.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.

try with the following line

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

Secondly, your Spring controller method should contain the pathVariable defined in the annotation:

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

For debugging:

put an error block in your ajax call.

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. Thanks for your time :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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