Angular学习之旅----post请求

先设置请求头import { Http, Jsonp, Headers } from '@angular/http';

private headers = new Headers({ 'Content-Type': 'application/json' })

postData() {

// 1.import { Http,Jsonp,Headers } from '@angular/http'; Headers 定义请求头的

//2.private headers = new Headers({'Content-Type': 'application/json'});

//3.post提交数据

var url = "http://127.0.0.1:3000/dologin";

this.http.post(url,

JSON.stringify({ "username": 'zhangsan', "age": '20' }),

{ headers: this.headers }).subscribe(function (data) {

console.log(data);

}, function (error) {

console.log(error);

})

}

 

import { Component, OnInit } from '@angular/core';
import { Http, Jsonp, Headers } from '@angular/http';
@Component({
  selector: 'app-news',
  templateUrl: './news.component.html',
  styleUrls: ['./news.component.css']
})
export class NewsComponent implements OnInit {
  title = '你还news' // 定义属性
  msg: any // 另一种定义属性的方法
  msg1: string = '这是string类型的数据'
  // 定义属性还可以加修饰符
  public username = '张三'
  public h = ''
  public list = []
  public obj = {
    name: '张三'
  }
  public list2 = []
  public list4: any[]
  public list5: any[]
  private headers = new Headers({ 'Content-Type': 'application/json' })
  /**
   * public 共有 * 默认 可以在这个类里面使用,也可以在类外面使用
   * protected 保护类型  他只有在当前类和他的子类里面可以访问
   * private 私有 只有在当前类型才可以访问这个属性
   */
  // 注入依赖服务
  constructor(private http: Http, private jsonp: Jsonp) {
    this.h = '<h2>后台数据</h2>'
    this.list = ['1', '2', '3']
    this.list2 = [
      { 'title': '111' },
      { 'title': '222' },
      { 'title': '333' }
    ]
    this.list4 = [

      {
        'catename': "宝马",

        "list": [

          { 'title': '宝马x1' },
          { 'title': '宝马x3' },
          { 'title': '宝马x2' },
          { 'title': '宝马x4' },
        ]

      }, {
        'catename': "奥迪",

        "list": [

          { 'title': '奥迪q1' },
          { 'title': '奥迪q2' },
          { 'title': '奥迪q3' },
          { 'title': '奥迪q4' },
        ]

      },
    ]
  }

  ngOnInit() {
  }
  requestData() {
    // alert('请求数据')
    var that = this;
    var url = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1";
    this.http.get(url).subscribe(function (data) {
      // console.log(JSON.parse(data['_body']))
      var list = JSON.parse(data['_body'])
      that.list5 = list['result']
      console.log(that.list5)
    }, function (err) {
      console.log(err)
    })
  }
  requestJsonpData() {
    // alert('请求数据')
    var that = this;
    var url = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1&callback=JSONP_CALLBACK";
    this.jsonp.get(url).subscribe(function (data) {
      // console.log(JSON.parse(data['_body']))
      console.log(data['_body'])
      // 此处不需要进行字符串解析
      var list = data['_body']
      that.list5 = list['result']
      console.log(that.list5)
    }, function (err) {
      console.log(err)
    })
  }
  postData() {
    // 1.import { Http,Jsonp,Headers } from '@angular/http';   Headers 定义请求头的
    //2.private headers = new Headers({'Content-Type': 'application/json'});
    //3.post提交数据
    var url = "http://127.0.0.1:3000/dologin";
    this.http.post(url,
      JSON.stringify({ "username": 'zhangsan', "age": '20' }),
      { headers: this.headers }).subscribe(function (data) {
        console.log(data);
      }, function (error) {
        console.log(error);
      })
  }
}
<div>
  {{title}}------{{msg1}}-----{{username}}
  <!-- 绑定属性 -->
  <span [title]='msg1'>绑定属性</span>
  <!-- 绑定后台的数据 -->
  <span [innerHtml]="h"></span>
  {{obj.name}}
  <!-- 循环数据 -->
  <ul>
    <li *ngFor="let item of list">{{item}}</li>
  </ul>
  <!-- <ul>
    <li *ngFor="let item of list2;let key=index">{{item.title}}---{{key}}</li>
  </ul> -->
  <ul>
    <li *ngFor="let item of list4;let key=index">{{item.catename}}---{{key}}
      <ol>
        <li *ngFor="let car of item.list">{{car.title}}</li>
      </ol>
    </li>
  </ul>
  <hr>
  <!-- <ul>
    <li template="ngFor let item of list2;let key=index">{{item.title}}---{{key}}</li>
  </ul> -->
  <!-- 不能用 -->
  <!-- <ul>
    <li template="ngFor let item of list5; let key=index">

      {{item.title}}------------{{key}}
    </li>
  </ul> -->
  <hr>
  <h2>请求数据</h2>
  <button (click)="requestData()">http get请求数据</button>
  <button (click)="requestJsonpData()">jsonp get请求数据</button>
  <button (click)="postData()">post提交数据</button>
  <ul>
    <li *ngFor="let item of list5">{{item.title}}</li>
  </ul>
</div>

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞朋哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值