【问题标题】:jquery.form get plain HTMLjquery.form 获取纯 HTML
【发布时间】:2014-01-31 12:57:26
【问题描述】:

我试图在插件 jquery.form.js (http://malsup.com/jquery/form/) 中获取纯 HTML 作为响应,但我无法得到它。所以我的代码看起来像这样:

print "$('#filter_form').ajaxForm({
            target:'#ajax1',
    beforeSend: function() {
      console.log('Image uploading started.');
      img_status.fadeOut();
      img_slider.width('0%');
      img_percent_text.html('0%');
    },
uploadProgress: function(event, position, total, percentComplete) {
  console.log('Image upload update...');
  img_slider.width(percentComplete + '%');
  img_percent_text.html(percentComplete + '%')
},

complete: function(responseText, statusText, xhr) {
    img_slider.width('100%');
    img_percent_text.html('100%')
    console.log('Image uploading finished!');
    img_preview.fadeOut(800);
    alert(responseText);
}

  });";

print "});";
print '</script>';

由于某种原因,这导致目标更改错误,请查看代码...

if (empty($_POST)) {
...... // I get this code as output
}
else{
print "this should be the output via ajax";
}

所以问题是:

如何在完整函数中获取纯HTML(responseText返回对象)

如何修复我通过 ajax 发送的 post 参数?

【问题讨论】:

  • 你如何在 jsfiddle 中测试表单,我没有看到 PHP 选项

标签: javascript php jquery ajax jquery-plugins


【解决方案1】:

注意empty 函数返回FALSE 如果你传入一个空数组()。

因此,如果您创建一个没有字段(参数)的 POST,那么 empty($_POST) 将返回 FALSE

如果要检查请求是否为 POST 类型,则检查 $_POST 变量是否使用 isset($_POST) 方法或 $_SERVER['REQUEST_METHOD'] === 'POST' 设置。

【讨论】:

  • 我使用它是因为表单操作指向表单所在的同一页面,所以如果没有发布查询我打印表单,如果有我只是处理提交的发布参数。我会试试isset()...
  • 好的,只是想指出,当您发布没有参数的帖子时,您的 if 语句将不起作用。但是,如果您希望始终有参数,那么这应该不是问题。
  • 谢谢,我会使用 REQUEST_METHOD :)
【解决方案2】:

您还可以将响应文本/html 对象附加到某些 HTML 实体,例如

$('#my_id').html(responseText);

<div id='my_id'></div>

【讨论】:

  • responseText 返回 [object Object],我已经尝试过了,并且 div 中的代码保持不变。
  • 刚刚发现在这个对象中有另一个 responseText,当我输入 responseText.responseText 时,它确实正确显示了输出,但是 post 查询又出错了
【解决方案3】:

所以这是我的错误:

1.如何以纯 HTML 格式获取响应?

responseText 是对象,其中包含 responseText。

Just use responseText.responseText\

我是如何发现的: 我用过

console.log(responseText)

在浏览器控制台输出对象,发现源码在responseText中。

2.post方法的问题

我认为这个插件从表单中获取方法,所以有两个修复 - 用参数强制它

type: 'POST'

$('...').ajaxForm({

或者简单地将表单方法设置为这样发布:

<form action="example.com" method="post" ></form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多