jQuery JSONP 호출 보내기

페이지:
<table id="resultTable">
        <tr class="tableHead">
            <th>  </th>
            <th>  </th>
            <th>   </th>
            <th>  IP</th>
            <th>   </th>
            <th>   </th>
            <th>basIP</th>
            <th>bas  </th>
            <th>   </th>
            <th>  </th>
            <th>  </th>
        </tr>
    </table>

JavaScript:
$('#queryButton').click(function () {
                log("Query Start");
                var typeStr = $('#type').val();
                var usernameStr = $('#username').val();
                var start = $('#startTime').val();
                var end = $('#endTime').val();
                var orderStr = $('#order').val();
                var isFuzzy = false;
                log("Check fuzzy");
                if (usernameStr.match(/^1[3|4|5|7|8][0-9]\d{4,8}$/)) {
                    log("Match Cellphone regex");
                    isFuzzy = false;
                } else {
                    log("Not Match Cellphone regex");
                    isFuzzy = true;
                }
                log("Got type:" + typeStr + " username:" + usernameStr + " start:" + start + " end:" + end + " order:" + orderStr + " fuzzy:" + isFuzzy);
                var URL = "http://localhost:8080/troubleshooting/service/troubleInfo";
                $.ajax({type: "get",
                    url: URL,
                    data: {type: typeStr, username: usernameStr, startTime: start, endTime: end, order: orderStr, fuzzy: isFuzzy},
                    dataType: "jsonp",
                    jsonpCallback: "troubleInfoCallback",
                    success: function (json) {
                        log("Query success");
                        $("tr").remove(".queryResult");
                        $.each(json, function (index, item) {
                            $("#resultTable").append("<tr class='queryResult'>" +
                                    "<td>" + index + "</td>" +
                                    "<td>" + item.infoType + "</td>" +
                                    "<td>" + item.username + "</td>" +
                                    "<td>" + item.userIP + "</td>" +
                                    "<td>" + item.belongsTo + "</td>" +
                                    "<td>" + item.accessFrom + "</td>" +
                                    "<td>" + item.basIP + "</td>" +
                                    "<td>" + item.basID + "</td>" +
                                    "<td>" + item.entryPoint + "</td>" +
                                    "<td>" + item.detail + "</td>" +
                                    "<td>" + item.requestdate + "</td></tr>");
                        });
                    }
                });
            });

서비스 포트:
@RequestMapping(value = "/troubleInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    @ResponseBody
    public String getTroubleInfo(@RequestParam(value = "callback", required = true) String callback,
                                 @RequestParam(value = "type", required = true) String msgType,
                                 @RequestParam(value = "username", required = false) String username,
                                 @RequestParam(value = "fuzzy", required = false) boolean fuzzy,
                                 @RequestParam(value = "startTime", required = false) String startTime,
                                 @RequestParam(value = "endTime", required = false) String endTime,
                                 @RequestParam(value = "order", required = false) String order) {
        DateTimeFormatter defaultDateFormatter = DateTimeFormat.forPattern(dateFormatterPattern);
        DateTime start, end;
        if (startTime != null && endTime != null && !startTime.isEmpty() && !endTime.isEmpty()) {
            start = defaultDateFormatter.parseDateTime(startTime);
            end = defaultDateFormatter.parseDateTime(endTime);
        } else {
            end = DateTime.now();
            start = end.minusDays(7).withTime(0, 0, 0, 0);
        }

        List<TroubleInfo> result = service.getInfo(msgType, username, fuzzy, start.toDate(), end.toDate(), order);
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
        gsonBuilder.setDateFormat(dateFormatterPattern);
        Gson gson = gsonBuilder.create();

        return callback + "(" + gson.toJson(result) + ")";
    }

설명: 서버 수신 매개 변수 콜백은 jsonpCallback: "troubleInfoCallback"입니다.
최종적으로 서비스 측에서 되돌아오는 것은 trouble Info Callback(JSONstr)이다.

좋은 웹페이지 즐겨찾기