jQuery ajax发送json并返回html [英] jQuery ajax send json and return html

查看:74
本文介绍了jQuery ajax发送json并返回html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用参数执行ajax调用.如果可能的话,我想将参数作为json或文本发送.

I want to perform an ajax call with parameters. I want to send the paramters as json or as text if this is possible.

结果以html内容类型返回.

The result is returned as html content type.

这就是我尝试过的

  var data2 = {
      'some-id': 5
  };

  $.ajax({
        type: "POST",
        url: /* some url */,
        data: JSON.stringify(data),
        dataType: 'json',
        success: function(data){      
            //some logic
        }
   }).fail(function() {
            //some error logic
   });

问题是,ajax失败,并显示消息"undefined",因为它期望html作为响应,但是我的操作返回了html.

The problem is, that the ajax fails with the message "undefined" because it expects html as response, however my action returns html.

如何使它与html响应一起使用?

How can I make this to work with html response?

推荐答案

只需将"dataType"设置为"html".

Just set the "dataType" to "html".

参数"dataType"是jQuery ajax调用期望返回的类型.

The parameter "dataType" is what type the jQuery ajax call expects in return.

更多信息: http://api.jquery.com/jQuery.ajax/

这篇关于jQuery ajax发送json并返回html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆