使用jsonp和ajax将表单数据作为GET参数发送

时间:2015-03-02 23:38:34

标签: php jquery ajax json jsonp

嗨,大家好,我是jsonp的新手。我试图做的只是发送简单的表单数据。我知道你不能使用jsonp POST,因为它是一个标准的脚本GET请求。下面我尝试了serialize()或serializeArray()表单数据,但GET请求中没有包含任何内容,请告诉我哪里出错了? HTML:

<form class="qd-bd-frm" name="qd-bd-frm" id="qd-bd-frm">
  <input type="hidden" value="9614d609b2b7987d734" name="uid" />
  <p>
    <textarea class="qd-input" placeholder="Your Message" name="msg"></textarea>
  </p>
  <p>
    <input type="button" class="qd-btn" value="Send" />
  </p>
</form>

Jquery的:

$.ajax({
    url: "http://www.cross-domain.com/send",
    dataType: "jsonp",
    data: $("#qd-bd-frm").serializeArray(),
    jsonpCallback: "sent",
    success: function (result)
    {
        alert(JSON.stringify(result));
    },
    complete: function (xhr, status)
    {
        alert(status);
    },
    error: function ()
    {

    }
 });

PHP:

public function send()
{
    header( "Content-Type: application/json" );
    echo "sent(".json_encode( $_GET ).");";

    die();
    // example response: sent({"callback":"sent","_":"1425338880075"});
}

如果你看到上面的内容,我只是回复了回复,在上面的成功函数中,没有包含任何表单输入。如果我检查firebug中的脚本选项卡,只有2个参数被发送到服务器,我也可以看到这个。我真正想念的是什么想法?

0 个答案:

没有答案