最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to build json data as formdata in angular 4 - Stack Overflow

programmeradmin0浏览0评论

How to send the below json as form data ? Tried JSON.stringify and its not working.

{
   name: 'starwars',
   year: 1977,
   data: [{ 
    'id': 'c5f6d301-328e-4167-8e13-504afb9a030e', 
    'item': 'bc4db36e-9e7c-478d-93a2-d4be32dacec1', 
    'qty': '1' 
  },
  { 
    'id': 'c5f6d301-328e-4167-8e13-504afb9a030e', 
    'item': 'bc4db36e-9e7c-478d-93a2-d4be32dacec1', 
    'qty': '1' 
  },
 ],
}

How to send the below json as form data ? Tried JSON.stringify and its not working.

{
   name: 'starwars',
   year: 1977,
   data: [{ 
    'id': 'c5f6d301-328e-4167-8e13-504afb9a030e', 
    'item': 'bc4db36e-9e7c-478d-93a2-d4be32dacec1', 
    'qty': '1' 
  },
  { 
    'id': 'c5f6d301-328e-4167-8e13-504afb9a030e', 
    'item': 'bc4db36e-9e7c-478d-93a2-d4be32dacec1', 
    'qty': '1' 
  },
 ],
}
Share Improve this question asked Sep 17, 2018 at 10:44 Krishnakumar CKrishnakumar C 1891 silver badge10 bronze badges 2
  • Where is the rest of your code? anyway I think you have to do json.parse() and pass it the json you've posted up here – Jacopo Sciampi Commented Sep 17, 2018 at 10:45
  • Angular has a whole guide about that. – user4676340 Commented Sep 17, 2018 at 10:52
Add a ment  | 

3 Answers 3

Reset to default 2
    var jsonData = {
   name: 'starwars',
   year: 1977,
   data: [{ 
    'id': 'c5f6d301-328e-4167-8e13-504afb9a030e', 
    'item': 'bc4db36e-9e7c-478d-93a2-d4be32dacec1', 
    'qty': '1' 
  },
  { 
    'id': 'c5f6d301-328e-4167-8e13-504afb9a030e', 
    'item': 'bc4db36e-9e7c-478d-93a2-d4be32dacec1', 
    'qty': '1' 
  },
 ],
};
var formData = new FormData();
Object.keys(jsonData).forEach((key)=>{formData.append(key,jsonData[key])});

Hope you are asking this. Now you can post the formData to your Rest Api.

import { Injectable, OnInit } from '@angular/core'
import { Http, Headers, URLSearchParams, RequestOptions } from '@angular/http'
import { Observable, Subject } from 'rxjs/Rx'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/catch'

@Injectable()
export class YifyMoviesProvider {
    constructor(public http: Http) {

    }
    postData(): Observable < any > {
        let jsonData = {
            name: 'starwars',
            year: 1977,
            data: [{
                    'id': 'c5f6d301-328e-4167-8e13-504afb9a030e',
                    'item': 'bc4db36e-9e7c-478d-93a2-d4be32dacec1',
                    'qty': '1'
                },
                {
                    'id': 'c5f6d301-328e-4167-8e13-504afb9a030e',
                    'item': 'bc4db36e-9e7c-478d-93a2-d4be32dacec1',
                    'qty': '1'
                },
            ],
        };
        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' })
        let options = new RequestOptions({
            headers: headers
        })
        let body = new URLSearchParams()
        body.set('mydata', JSON.stringify(jsonData))
        return this.http.post(`yourip`, body, options).
        map(res => res.json())
        .catch((error: any) => {
            return Observable.throw(new Error(error.status))
        })
    }
}

Incase if you want to post the json data to Rest api as form data this would work in angular 4!

 var jsonData = {
   'name': 'starwars1',
   'year': 1977,
   'data': [{ 
    'id': 'c5f6d301328416e', 
    'item': 'bc4db36e-9e7c-478d-93a2-d4be32dacec1', 
    'qty': '1' 
  },
  { 
    'id': 'c5f6d301-328e-4167-8e13-504afb9a030e', 
    'item': 'bc4db36e-9e7c-478d-93a2-d4be32dacec1', 
    'qty': '1' 
  },
 ],
};
发布评论

评论列表(0)

  1. 暂无评论