为什么这个http请求在角度4中失败,但是却经过提琴手? [英] Why is this http request failing in angular 4, but goes through with fiddler?

查看:108
本文介绍了为什么这个http请求在角度4中失败,但是却经过提琴手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过提琴手发出的http发帖请求:使用

http://localhost:58183/api/Account/Register

正文为

{"email":"jbjunk4@outlook.com","password":"Abc1231*","confirmPassword":"Abc1231*"}

这执行得很好,并且电子邮件已在webapi 2的身份数据库中注册.

但是,我的authorize.service.ts中包含以下代码:

 registerUser(localUser: LocalUser) {
        var headers = new Headers();
        headers.append('Content-Type', this.constants.jsonContentType);
        var creds = JSON.stringify(localUser);
        console.log(creds);
        console.log(this.constants.accountUrl + "Register");

        return this.http.post(this.constants.accountUrl + "Register", creds, { headers: headers })
            .map(res => res.json());
    }

creds = 
{"email":"jbaird9@arsbirder.com","password":"Abc1231*","confirmPassword":"Abc1231*"}

url = http://localhost:58183/api/Account/Register
json-contentType = "application/json;charset=UTF-8";

控制台显示: SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝.

首页 HTML1300:发生导航. 家 模板解析警告: 元素已弃用.改用("-child ui-shadow':!root}" class ="ui-menu-list" (click)="listClick($ event)"> [警告->] http://localhost:58183/api/Account/Register authorization.service.ts(47,9) SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝.

首页 错误语法错误:无效字符 core.es5.js(1020,1) 错误" { [功能]: , proto :{}, 描述:无效字符", 消息:无效字符", 名称:"SyntaxError", 编号:-2146827274, 堆栈:"SyntaxError:无效字符 在匿名函数中( http://localhost:4200/main.bundle.js:788: 13 ) 在SafeSubscriber.prototype .__ tryOrUnsub( http://localhost:4200/vendor.bundle.js :37370:13 ) 在SafeSubscriber.prototype.error( http://localhost:4200/vendor.bundle.js :37329:21 ) at Subscriber.prototype._error( http://localhost:4200/vendor.bundle.js :37260:9 ) at Subscriber.prototype.error( http://localhost:4200/vendor.bundle.js :37234:13 ) at Subscriber.prototype._error( http://localhost:4200/vendor.bundle.js :37260:9 ) at Subscriber.prototype.error( http://localhost:4200/vendor.bundle.js :37234:13 ) 在onError( http://localhost:4200/vendor.bundle.js:108672:17 ) 在ZoneDelegate.prototype.invokeTask( http://localhost:4200/polyfills.bundle.js :2836:13 ) 在onInvokeTask( http://localhost:4200/vendor.bundle.js:90272:21 )" }

HTTP405:错误的方法-不支持使用HTTP动词. (XHR)选项- http://localhost:58183/api/Account/Register

我不知道这一点.预先感谢您的帮助.

解决方案

我将cors属性添加到了webapi配置中...一切都开始工作... var cors = new EnableCorsAttribute(",","GET ,PUT,POST,PATCH,DELETE,OPTIONS); config.EnableCors(cors);

I have an http post request through fiddler: using

http://localhost:58183/api/Account/Register

with a body of

{"email":"jbjunk4@outlook.com","password":"Abc1231*","confirmPassword":"Abc1231*"}

This executes just fine and the email is registered in the identity database on webapi 2.

However, I have the following code in my authorize.service.ts:

 registerUser(localUser: LocalUser) {
        var headers = new Headers();
        headers.append('Content-Type', this.constants.jsonContentType);
        var creds = JSON.stringify(localUser);
        console.log(creds);
        console.log(this.constants.accountUrl + "Register");

        return this.http.post(this.constants.accountUrl + "Register", creds, { headers: headers })
            .map(res => res.json());
    }

creds = 
{"email":"jbaird9@arsbirder.com","password":"Abc1231*","confirmPassword":"Abc1231*"}

url = http://localhost:58183/api/Account/Register
json-contentType = "application/json;charset=UTF-8";

The console shows: SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.

home HTML1300: Navigation occurred. home Template parse warnings: The element is deprecated. Use instead ("-child ui-shadow':!root}" class="ui-menu-list" (click)="listClick($event)"> [WARNING ->] http://localhost:58183/api/Account/Register authorization.service.ts (47,9) SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.

home ERROR SyntaxError: Invalid character core.es5.js (1020,1) "ERROR" { [functions]: , proto: { }, description: "Invalid character", message: "Invalid character", name: "SyntaxError", number: -2146827274, stack: "SyntaxError: Invalid character at Anonymous function (http://localhost:4200/main.bundle.js:788:13) at SafeSubscriber.prototype.__tryOrUnsub (http://localhost:4200/vendor.bundle.js:37370:13) at SafeSubscriber.prototype.error (http://localhost:4200/vendor.bundle.js:37329:21) at Subscriber.prototype._error (http://localhost:4200/vendor.bundle.js:37260:9) at Subscriber.prototype.error (http://localhost:4200/vendor.bundle.js:37234:13) at Subscriber.prototype._error (http://localhost:4200/vendor.bundle.js:37260:9) at Subscriber.prototype.error (http://localhost:4200/vendor.bundle.js:37234:13) at onError (http://localhost:4200/vendor.bundle.js:108672:17) at ZoneDelegate.prototype.invokeTask (http://localhost:4200/polyfills.bundle.js:2836:13) at onInvokeTask (http://localhost:4200/vendor.bundle.js:90272:21)" }

HTTP405: BAD METHOD - The HTTP verb used is not supported. (XHR)OPTIONS - http://localhost:58183/api/Account/Register

I can't figure this out. Thanks in advance for any help.

解决方案

I added the cors attributes to webapi config... and it all started working... var cors = new EnableCorsAttribute("", "", "GET,PUT,POST,PATCH,DELETE,OPTIONS"); config.EnableCors(cors);

这篇关于为什么这个http请求在角度4中失败,但是却经过提琴手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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