带有http-fetch-client的Aurelia Post产生一个选项请求 [英] Aurelia Post with http-fetch-client producing an options request

查看:172
本文介绍了带有http-fetch-client的Aurelia Post产生一个选项请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小型论坛,公司中的人们可以使用aurelia即时投放要出售的商品或服务的广告.我有一个工作正常的广告页面列表,每个广告工作的详细信息页面都使用来自api的获取请求.但是,当有人要在广告上添加评论时,我似乎无法获得Post的要求.

I'm creating a small forum where people in our company can put up adverts for goods or services they want to sell on the fly, using aurelia. I have a list of adverts page working fine, a details page for each advert working fine both using get requests from an api. However i can't seem to get the work the Post reqeust when someone wants to add a comment on an advert.

@inject(HttpClient)
export class ApiData {
    constructor(httpClient) {
        httpClient.configure(config => {
            config
                .withBaseUrl("MyUrl");
        });
        this.http = httpClient;
        //.configure(x => {x.withHeader('Content-Type', 'application/json');});
    }

    postAdvertComment(comment, id) {
        return this.http.fetch(`/adverts/${id}/comments`, {
            method: "post",
            body: json(comment),
            headers: {
                'Accept': 'application/json'
            }
        });
    } 

    getAdverts() {
        return this.http.fetch("/adverts")
            .then(response => {
                return this.adverts = response.json();
            });
    }

    getAdvert(id) {
        return this.http.fetch(`/adverts/${id}`)
            .then(response => {
                return this.advert = response.json();
            });
    }
}

在执行此项目时,我们在CORS方面遇到了一些问题,所有问题都可以通过在api中添加AllowCors标签(包括所有方法等)来解决.

Doing this project we've had some issue with CORS, all solved by adding in AllowCors tags in the api, including all methods etc.

<add key="CorsAllowedOrigins" value="*" />
<add key="CorsAllowedHeaders" value="" />
<add key="CorsAllowedMethods" value="*" />

但是,当我尝试运行该帖子时,它会运行一个options方法并返回一个400 Bad请求. 此处

However when i try and run the post, its running an options method and returns a 400 Bad request. Here

我们还会收到以下CORS错误:

We also get the following CORS error:

Fetch API cannot load MyURL/api/adverts/2/comments. Response to preflight
request doesn't pass access control check: No 'Access-Control-Allow-Origin'
header is present on the requested resource. Origin 'http://localhost:49877' is 
therefore not allowed access. The response had HTTP status code 400. If an 
opaque response serves your needs, set the request's mode to 'no-cors' to fetch 
the resource with CORS disabled.

我不知道这是我们的C#API还是我尝试从aurelia发布的问题,但是我们尝试了发送来自邮递员的请求,并且效果很好,尝试使用同一应用程序在同一应用程序中发送发布请求jquery可以正常工作,所有的get请求都可以正常工作,但是由于某种原因,这篇文章引起了各种各样的问题.

I don't know if it's a problem with our c# api or with how I'm trying to post from aurelia, but we have tried sending requests from postman and it works fine, tried sending post request within the same app using jquery and it works fine, and all the get requests work fine, but for some reason this post is causing all sorts of problems.

推荐答案

您的WebAPI中似乎存在问题,但是在向您提供一些可能的解决方案之前,我想向您介绍一些重要的事情.

It seems to be a problem in your WebAPI, but before giving you some possible solutions I'd like to show you some important things.

  • 邮递员不受CORS的影响,因此所有请求均有效.
  • jQuery ajax使用XHR(XmlHttpRequest对象),而aurelia-fetch-client使用fetch(window.fetch.但是,fetch-polyfill在后台使用XHR).他们是 解决同一问题的不同方法.仅仅因为其中一个起作用,实际上并不意味着另一个也应该起作用.
  • OPTIONS请求是通过fetch发出的,这就是它的工作方式.有关更多信息,请参见 https://developers.google .com/web/updates/2015/03/introduction-to-fetch?hl = zh-CN
  • Postman is not affected by CORS, so all requests work.
  • jQuery ajax uses XHR (XmlHttpRequest object) while aurelia-fetch-client uses fetch (window.fetch. However, the fetch-polyfill uses XHR in the background). They are different approaches to solve the same problem. Just because one of them work, doesn't actually mean that the other one should work too.
  • The OPTIONS request is made by fetch, that's how it works. More information here https://developers.google.com/web/updates/2015/03/introduction-to-fetch?hl=en

要解决此问题,请尝试从web.config中删除这些标签,并在Startup.cs中允许CORS.像这样:

To solve this problem try to remove those tags from web.config, and allow CORS in your Startup.cs. Like this:

public void Configuration(IAppBuilder app)
{
    app.UseCors(CorsOptions.AllowAll); //or another parameter
    //rest of your code
}

您不必将content-type标头设置为application/json.当您使用json()功能---> body: json(comment)

You don't have to set the content-type header to application/json. It is automatically made when you use the json() function ---> body: json(comment)

如果您使用的是OWIN,则可能必须将内容类型发送为x-www-form-urlenconded.在这种情况下,请查看此通过aurelia-fetch-client发布"x-www-form-urlencoded"内容

If you are using OWIN you might have to send the content-type as x-www-form-urlenconded. In that case, take a look at this Post 'x-www-form-urlencoded' content with aurelia-fetch-client

这篇关于带有http-fetch-client的Aurelia Post产生一个选项请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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