javascript - Controller 可以提供 json 或 html

标签 javascript jquery

问题是 Controller 可以给出 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);
            }
        }
    });
});

最佳答案

另一个解决方案...在这里找到: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
            } 
        }
    });
});  

关于javascript - Controller 可以提供 json 或 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16650868/

相关文章:

javascript - 获取绘制点的半径大小(以米为单位)

javascript - 附加第二个子项,更改其颜色,innerhtml

javascript - JS漏洞程度

javascript - 有没有办法阻止 IE8 'pause' 因为 ajax 响应是由 jquery/javascript 处理的,如果没有,为什么?

php - 无法获取 google recaptcha v2 以防止提交表单

javascript - 在可拖动的 div 中设置正确的位置

javascript - Google 登录在注销时删除 cookie

jquery - `$container.imagesLoaded is not a function` 错误

javascript - 如何找出 img-tag 是否留在 html 编辑器中的插入符号节点?

javascript - do while 循环在 Titanium 中无法正常工作