如何使用角度4对azure函数发出请求? [英] How to make a post request to azure function using angular 4?

查看:68
本文介绍了如何使用角度4对azure函数发出请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用angular 2代码发送了Azure Https POST请求。

I had sent the Azure Https POST request using angular 2 code.

vendordata(){

  let input = {
    VendorName: this.vname,
    Address: this.add,
    store: this.opt
  };

  let headers = new Headers();
  headers.append('Content-Type', 'application/json');

  this.http.post('https://dxxxxxxxxxxx', input, {headers: headers })
    .subscribe(data => {
      this.value = data;
      console.log(this.value);

    });

  }

并收到以下错误:


错误:预检的响应包含无效的HTTP状态代码400

Error: Response for preflight has invalid HTTP status code 400


推荐答案

我用angular 4测试了你的代码并且它可以工作。

I tested your code with angular 4 and it works.

import { Component, OnInit } from '@angular/core';
import { Http, Headers } from '@angular/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app';

  constructor(private http: Http) {}

  ngOnInit(): void {

    const functionURI = 'https://<functionname>.azurewebsites.net/api/HttpTriggerJS1?code=<code>';  

    let input = {
      name: "Azure",
    };

    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    this.http.post(functionURI, input, { headers: headers } ).subscribe(data => {
      console.log(data);
    });
  }
}

这是我使用Javascript的简单HTTP触发函数:

Here is my simple HTTP trigger function using Javascript:

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "Hello " + (req.query.name || req.body.name)
        };

    } else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
    context.done();
};

CORS设置:

浏览器的输出:

这篇关于如何使用角度4对azure函数发出请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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