在多部分Ajax Angular调用之后解析JSON正文 [英] Parse a JSON body after multipart ajax Angular call

查看:130
本文介绍了在多部分Ajax Angular调用之后解析JSON正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ajax angularJs进行api调用后解析即将出现的主体. 呼叫后,响应为:

I'm trying to parse a body that is coming to me after an api call using ajax angularJs. After call the response is:

--3531b7e68196e3144197f82db0864b7e391c8b0ad51c4176c28f8ac41b3c
Content-Disposition: form-data; name="passport"; filename="passport.json"
Content-Type: application/json

{
    "name": "Nothing",
    "dob_display": "10/11/1997",
    "dob_accuracy": "FD",
    "owner_firstname": "Nothing",
    "owner_surname": "To Understand"
}
--3531b7e68196e3144197f82db0864b7e391c8b0ad51c4176c28f8ac41b3c--

我没有找到从该请求获取正文的插件.我是否需要进行手动解析器,否则可以获得其他解决方案. 有人能帮我吗?

I didn't find a plugin to get the body from this request. Do I need to make manual parser or I could get another solution. Can someone help me?

推荐答案

这是我解析API调用后出现的任何表单数据的解决方案:

This is my solution for parsing any form data that is coming after an API call:

parser = function (data) {
  // this will split --1491test9246asaery134214
  // if you have multiple files in the response
  var dataArray = data.split(/--\S*[0-9a-z]/g), response = {};
  underscore.each(dataArray, function (dataBlock) {
    var rows = dataBlock.split('\n'),
        header = rows.splice(0, 4).slice(1, 3),
        body = rows.join('');

    if (header.length > 1) {
      var patternGetName = /(".*?")/g,
          name = patternGetName.exec(header[0])[0].replace(/(")/g, '');
      response[name] = body;
    }
  });
  return response;
};

这篇关于在多部分Ajax Angular调用之后解析JSON正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆