尝试将表单发布到Facebook时出现400(错误请求)错误VIA Ajax& jQuery的

时间:2012-06-28 07:32:20

标签: php ajax facebook jquery

我正在尝试使用我网站上的表单将图片发布到Facebook相册。我知道我得到了正确的访问令牌,因为没有jQuery和AJAX的帖子工作正常 - 但重定向我的网站。为了解决这个问题,我做了一个jQuery AJAX帖子,但这给了我一个我不明白的错误。

我正在使用:

<script type="text/javascript" src="../Components/JQUERY/jquery-1.4.2.min.js"></script>    
<script type="text/javascript" src="../Components/JQUERY/jquery.validate.min.js"></script>

代码是:

  1. 获取访问令牌并准备要发布到的网址(没关系,不使用jQuery发帖将图片上传到Facebook):

    //....Facebook code for getting the access token...//
    
    // This is the URL that was originally in the form's action tag//
    $image_url= "https://graph.facebook.com/" . $ALBUM_ID . "/photos?"
    . "access_token=" .$access_token;
    
  2. 表单的HTML:

    <form name="myform" id="myform" enctype="multipart/form-data" action="" method="POST">
       Please choose a photo:
         <input name="source" type="file"><br/>
       Say something about this photo: <br/>
         <textarea id="fbText" name="message" rows="4" cols="47"> </textarea><br/><br/>
         <input type="submit" name="submit" value="Upload"/><br/>
    </form>
    
    <div id="results"></div>
    
  3. jQuery代码:

    <script type="text/javascript">
      $(document).ready(function(){
          $("#myform").validate({
              debug: false,
              rules: {
                  message: "required"   
              },
              messages: {
                  message: "Please insert text."
              },
              submitHandler: function(form) {
                  // do other stuff for a valid form
                  $.post('<?php echo $image_url ?>', $("#myform").serialize(), function(data) {
                    $('#results').html(data);
                });
            }
        });
    });
    </script>
    
  4. 按上传按钮会出现以下错误:

    enter image description here

    我做错了什么? 感谢。

1 个答案:

答案 0 :(得分:1)

您无法使用jquery post方法发布多部分表单数据。还有一个问题是跨域通信。您无法将ajax请求发送到其他域。

有关替代方案,请参阅

  

How do you post to an iframe?
  http://jquery.malsup.com/form/#file-upload

     

Facebook new javascript sdk- uploading photos with it!

要通过ajax发送多部分数据,请参阅这些链接

  

Making an HTTP POST call with multipart/form-data using jQuery?

     

Sending multipart/formdata with jQuery.ajax

或最后,您可以上传到您的服务器并上传到Facebook ..

<强> [编辑]

var options = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback      
    }; 

    // bind form using 'ajaxForm' 
    $('#myForm1').ajaxForm(options); 
相关问题