Home > Backend Development > PHP Tutorial > [又拍云]通过jquery调用表单API出现401错误

[又拍云]通过jquery调用表单API出现401错误

WBOY
Release: 2016-06-06 20:44:54
Original
1520 people have browsed it

源代码如下:

<code class="lang-javascript">
  
    <meta charset="utf-8">
    <script src="../qddian/js/jquery.js"></script>
  
  
    <form action="http://v0.api.upyun.com/<?php%20echo%20%24bucket?>" method="post" enctype="multipart/form-data">
      <!-- 需要传递以下三个表单内容 -->
      <input type="hidden" name="policy" value="<?php echo $policy?>">
      <input type="hidden" name="signature" value="<?php echo $signature?>">
      <input type="file" name="file">
      <a id="upload" href="#">上传</a>
    </form>
  

<script>
    $(function(){
        $('a#upload').click(function(){
            //$('form').submit();

            $.post(
                'http://v0.api.upyun.com/<?php echo $bucket; ?>',
                {
                    file: $('input[name=file]').val(),
                    policy: $('input[name=policy]').val(),
                    signature: $('input[name=signature]').val()
                },
                function(data){
                    alert('Data: ' + data);
                }
            );

            return false;
        });
    });
</script>
</code>
Copy after login
Copy after login

通过上述方式则提示"401 Unauthorized sign error",但通过$('form').submit()方式提交则返回200成功。截图如下,请各位大牛协助解决一下,不胜感激!

[又拍云]通过jquery调用表单API出现401错误
[又拍云]通过jquery调用表单API出现401错误
[又拍云]通过jquery调用表单API出现401错误

回复内容:

源代码如下:

<code class="lang-javascript">
  
    <meta charset="utf-8">
    <script src="../qddian/js/jquery.js"></script>
  
  
    <form action="http://v0.api.upyun.com/<?php%20echo%20%24bucket?>" method="post" enctype="multipart/form-data">
      <!-- 需要传递以下三个表单内容 -->
      <input type="hidden" name="policy" value="<?php echo $policy?>">
      <input type="hidden" name="signature" value="<?php echo $signature?>">
      <input type="file" name="file">
      <a id="upload" href="#">上传</a>
    </form>
  

<script>
    $(function(){
        $('a#upload').click(function(){
            //$('form').submit();

            $.post(
                'http://v0.api.upyun.com/<?php echo $bucket; ?>',
                {
                    file: $('input[name=file]').val(),
                    policy: $('input[name=policy]').val(),
                    signature: $('input[name=signature]').val()
                },
                function(data){
                    alert('Data: ' + data);
                }
            );

            return false;
        });
    });
</script>
</code>
Copy after login
Copy after login

通过上述方式则提示"401 Unauthorized sign error",但通过$('form').submit()方式提交则返回200成功。截图如下,请各位大牛协助解决一下,不胜感激!

[又拍云]通过jquery调用表单API出现401错误
[又拍云]通过jquery调用表单API出现401错误
[又拍云]通过jquery调用表单API出现401错误

401的错误是签名的问题:
401:Unauthorized 访问未授权
401:Sign error 签名错误(操作员和密码,或签名格式错误)
sign error:
检查签名格式是否和又拍接口文档中一致。
Unauthorized:
错误检查空间名,操作员,密码等信息填写是否正确,上传路径是否有根“/”开始。
你可以参考下以上错误提示。

这个错误详细查看upyun的API http://www.upyun.com/intro/api-errors.php

另外使用jQuery的post是无法上传文件的,

<code class="lang-javascript">$('input[name=file]').val()
</code>
Copy after login

获取的只是上传文件的本地路径。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template