javascript - 尝试在查询参数中发送 base64 图像字符串时收到 400 Bad Request?

标签 javascript angular http post base64

尝试为查询参数中的图像发送 base64 字符串,并收到 400 错误请求。 尝试了以下操作:-

通过堆栈溢出这个问题。 据此,chrome 可以在 URL 中包含比我作为 imageBytes(29527 个字符)发送的字符更多的字符,

What is the maximum possible length of a query string ?

以下问题中的说明也没有帮助

Passing base64 encoded strings in URL ?

这是我的代码

如果我发送一个像这样的硬编码字符串来代替 form.imageBytes,它会工作正常

$scope.submit = function(){
  var form = {};
  form.name = document.getElementById("recipient-name").value;
  form.desc = document.getElementById("message-text").value;
  form.owner = document.getElementById("message-text2").value;
  form.imageBytes = $scope.asdf;

  var data = JSON.stringify(form);

    debugger;
    var postString = "http://cos1plp:7030/tcw/interestgroup/addInterestGroups?name="+form.name+"&desc="+form.desc+"&owner="+form.owner+"&imageBytes="+form.imageBytes;

    $http.post(postString)

        .success(function (data, status, headers, config) {
        $scope.headers = headers();
        console.log('Response Headers:' + headers());
        })

        .error(function (data, status, headers, config) {
        $scope.headers = headers();
        });

}

最佳答案

Base64字符串可以包含特殊字符,即+/=符号。您需要将它们编码为有效的 url 字符串。尝试将此添加到您的代码中:

data = data.replace(/\+/g, '%2B');
data = data.replace(/\//g, '%2F');
data = data.replace(/\=/g, '%3D');

关于javascript - 尝试在查询参数中发送 base64 图像字符串时收到 400 Bad Request?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43546721/

相关文章:

git - 如何解决 Dokku 部署中的 pre-receive hook denied 错误?

c# - 如何 : BeginProcessRequest

perl - 如何在 Perl 中拦截 HTTP 请求?

Http 协议(protocol)内容长度

javascript - 如何在 react 中制作阅读更多按钮?

javascript - Google 标记动画更改

javascript - jQuery 滚动条与点击计数器脚本冲突(document.write)

javascript - Angular 2 : attaching string with property binding

javascript - 从 Knockout 发布到 ASP.NET MVC Controller

typescript - 类型错误 : Cannot read property 'Symbol(Symbol.iterator)' of undefined