0

When I directly type the url for json response in browser I get the correct response printed in browser in JSON but with same URL is called by clicking on a button then response looks to be empty.

Here is my controller:

@RequestMapping(value="/ShowAllCustomers", method = RequestMethod.GET)
    public @ResponseBody Customer AllCustomersList() {
            Customer customer=customerService.findById(1); 


        return customer;
    }

And here I am trying to get response:

<script type="text/javascript">  
function startAjax() {
    $.ajax({
        type : 'GET',
        url : 'customers/ShowAllCustomers',//this is url mapping for controller
        dataType: 'json', 
        success : function(response) { 
                alert(response);
                alert(response.first_name);
                         //this response is list of object commming from server
        }
    });
}
</script> 

Here is button code which is pressed:

<input type="button" value="Customers" onclick="startAjax();"/>

But when I click on button I see no alert message on screen.

Any solution please?

4

1 回答 1

0

首先验证jQuery调用的URL和你在浏览器中手动调用的URL是一样的。

您可以在 Chrome 中执行此操作,方法是按 F12,单击“网络”选项卡,单击按钮后,验证 URL 是否被调用并且是正确的,然后单击它验证是否有任何响应。

在 Java 端,您可以在调用方法时添加日志记录,但首先我认为您最好验证 JavaScript 端发生的情况。

还要检查控制台选项卡,您可能会遇到 JavaScript 错误,这会阻止进一步的代码执行。

祝你好运。

于 2013-06-17T23:47:27.807 回答