Web服务Spring ajax调用

Web service Spring ajax call

本文关键字:调用 ajax Spring 服务 Web      更新时间:2023-09-26

我正在尝试使用ajax调用web服务。服务已经启动,它可以在firefox的RestClient上显示结果,但是,在我的应用程序调用中,给我状态错误"Pending"。

这是一个简单的web服务。

@Controller
@RequestMapping("/hello")
public class HelloWs {

@RequestMapping(value= "/helloWorld", method = RequestMethod.GET, headers = "Accept=application/xml, application/json")
        public @ResponseBody String  HelloWorld() {

            return "Hello Worldssss¡¡";
        }

这是我的ajax调用

function hellowsfunction() {
    $.ajax({
        type: "GET",
        url:"http://localhost:8080/ehCS-ui/rest/hello/helloWorld",
        crossDomain: true,
        dataType: "JSON",
        headers : {Accept : "applicationjson","Access-Control-Allow-Origin" : "*"},
            success: function(msg) {
             var returnedData = jQuery.parseJSON(msg);
             $("#lblResult")
            .text(result)
            .slideUp("hide", function() { $(this).slideDown("slow") });
     },
      error: function (e) { 
            $("#lblResult").removeClass("loading");
            alert('failed:'+e);
            console.log(e);
             }
     });

怎么了?想法吗?请帮忙。由于

你的@RequestMapping是错误的…你不应该像这样基于Accept标头进行映射。您应该使用produces参数。

@RequestMapping(value="/helloWorld", method=RequestMethod.GET, 
    produces={"application/xml", "application/json"})

也你的标题在JS是不正确的。