jquery.form获取纯HTML

时间:2014-01-31 12:57:27

标签: javascript php jquery ajax jquery-plugins

我试图在插件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发送的帖子参数?

3 个答案:

答案 0 :(得分:1)

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

因此,如果您在没有字段(params)的情况下进行POST,则empty($_POST)将返回FALSE

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

所以这是我的错误:

1.如何将响应视为纯HTML?

responseText是object,包含responseText。

Just use responseText.responseText\

我是怎么发现的: 我用了

console.log(responseText)

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

2. post方法的问题

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

type: 'POST'

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

或者只是将表单方法设置为发布方式:

<form action="example.com" method="post" ></form>
相关问题