最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Intelijj IDEA state, that success function in AJAX request in never used. - Stack Overflow

programmeradmin1浏览0评论

I have a problem and I have no idea what the reason is. I'm testing ajax requests with this code.

    function sendAJAX() {
            var dataToSend = {};
            dataToSend["username"] = $("#username").val();
            dataToSend["age"] = $("#age").val();
            dataToSend["date"] = $("#date").val();

            $.ajax({
                type : "POST",
                contentType : "application/json",
                url : "dotheajax",
                data : JSON.stringify(dataToSend),
                dataType : "json",
                success : function(response) {
                    $("#typeAjaxHere").html(response);
                }
            });
        }

        $("#form").submit(function (event) {
            event.preventDefault();
            sendAJAX();
        })

<div id="form">
<form id="user_form">
    <label for="username">Name</label>
    <input type="text" name="username" id="username">
    <label for="age">Age</label>
    <input type="text" name="age" id="age">
    <label for="date">Birth date</label>
    <input type="text" name="date" id="date">
    <input type="submit" value="Submit">
</form>


@Controller
public class AjaxControllers {
  @RequestMapping(value = {"dotheajax"}, method = RequestMethod.POST)
  public @ResponseBody String testAjax(@RequestBody HumanDomain humanDomain) {
    System.out.println(humanDomain.getUsername());
    System.out.println(humanDomain.getAge());
    System.out.println(humanDomain.getDate());
    return "Success";
  }
}

public class HumanDomain {

String username;

int age;

String date;

//getters and setters here
}

IntelliJ IDEA marks the success in AJAX as "unused property success" and nothing happens obviously in the "success body". I really don't know why. The request is working fine, in console I get the awaited result. My other similar AJAX function works, but I don't send any JSON data there and it is GET instead of POST. Any advice will be really appreciated. P.S. The error and done are also marked as unused.

I have a problem and I have no idea what the reason is. I'm testing ajax requests with this code.

    function sendAJAX() {
            var dataToSend = {};
            dataToSend["username"] = $("#username").val();
            dataToSend["age"] = $("#age").val();
            dataToSend["date"] = $("#date").val();

            $.ajax({
                type : "POST",
                contentType : "application/json",
                url : "dotheajax",
                data : JSON.stringify(dataToSend),
                dataType : "json",
                success : function(response) {
                    $("#typeAjaxHere").html(response);
                }
            });
        }

        $("#form").submit(function (event) {
            event.preventDefault();
            sendAJAX();
        })

<div id="form">
<form id="user_form">
    <label for="username">Name</label>
    <input type="text" name="username" id="username">
    <label for="age">Age</label>
    <input type="text" name="age" id="age">
    <label for="date">Birth date</label>
    <input type="text" name="date" id="date">
    <input type="submit" value="Submit">
</form>


@Controller
public class AjaxControllers {
  @RequestMapping(value = {"dotheajax"}, method = RequestMethod.POST)
  public @ResponseBody String testAjax(@RequestBody HumanDomain humanDomain) {
    System.out.println(humanDomain.getUsername());
    System.out.println(humanDomain.getAge());
    System.out.println(humanDomain.getDate());
    return "Success";
  }
}

public class HumanDomain {

String username;

int age;

String date;

//getters and setters here
}

IntelliJ IDEA marks the success in AJAX as "unused property success" and nothing happens obviously in the "success body". I really don't know why. The request is working fine, in console I get the awaited result. My other similar AJAX function works, but I don't send any JSON data there and it is GET instead of POST. Any advice will be really appreciated. P.S. The error and done are also marked as unused.

Share Improve this question asked Nov 4, 2015 at 17:33 DinjvaldDinjvald 711 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

I see the same error but it seems to be superficial, meaning it doesn't affect the functionality.

To make the error go away, I surrounded the function with brackets, making it an array containing one function, and it validates.

Example:

$.ajax({
    type : "POST",
    contentType : "application/json",
    url : "dotheajax",
    data : JSON.stringify(dataToSend),
    dataType : "json",
    success : [
        function(response) {
            $("#typeAjaxHere").html(response);
        }
    ]
});

From JQuery documentation,

"As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn."

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论