0

問題は、コントローラーがjsonまたはhtmlピースを提供できることです。それが何であるかを知る方法は?

$(document).on("submit", "form.client-form", function () {
    $.ajax({
        type: this.method,
        url: this.action,
        data: $(this).serialize(),
        success: function (result) {
            if (result is json) { 
                ... 
            } else if (result is html) {
                $("#result").html(result);
            }
        }
    });
});
4

3 に答える 3

2

別の解決策...ここにあります:jquery how to check response type for ajax call

$(document).on("form.client-form", "submit", function () {
    $.ajax({
        type: this.method,
        url: this.action,
        data: $(this).serialize(),
        success: function(result, status, xhr){ 
            var ct = xhr.getResponseHeader("content-type") || "";
            if (ct.indexOf('html') > -1) {
                //html here
                $("#result").html(result);
            }
            if (ct.indexOf('json') > -1) {
                //json here
            } 
        }
    });
});  
于 2013-05-20T14:27:26.393 に答える
0
$(document).on("form.client-form", "submit", function () {
    $.ajax({
        type: this.method,
        url: this.action,
        data: $(this).serialize(),
        success: function (result) {
            try {
                var response = $.parseJSON(result); 
            }
            catch (ex){
                //something else
                $("#result").html(result);
            }
        }
    });
});
于 2013-05-20T14:00:08.190 に答える
0

コンバーターの使用はどうですか?jquery ajax api でのコンバーターの使用をご覧ください: http://api.jquery.com/jQuery.ajax/

于 2013-05-20T14:02:20.450 に答える