Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
角度2类型错误:对象不';t支持属性或方法';json';_Json_Asp.net Mvc_Angular_Typescript - Fatal编程技术网

角度2类型错误:对象不';t支持属性或方法';json';

角度2类型错误:对象不';t支持属性或方法';json';,json,asp.net-mvc,angular,typescript,Json,Asp.net Mvc,Angular,Typescript,我正在使用Angular 2前端和MVC后端构建一个引用SSMS数据库的基本CRUD应用程序。我在任何浏览器上运行HTTPGet时遇到错误: TypeError:对象不支持属性或方法“json” 我为服务调用设置了一个调试点,但它没有到达那里,所以我假设问题出在组件的某个地方 我在谷歌上搜索了大约一天半的时间,发现很多人用其他东西来代替“json”,但我找不到其他人有这个问题。在下面你会找到我要的罐头。如果您还需要其他代码片段,请告诉我 这是我的Get组件: import { Component

我正在使用Angular 2前端和MVC后端构建一个引用SSMS数据库的基本CRUD应用程序。我在任何浏览器上运行HTTPGet时遇到错误:

TypeError:对象不支持属性或方法“json”

我为服务调用设置了一个调试点,但它没有到达那里,所以我假设问题出在组件的某个地方

我在谷歌上搜索了大约一天半的时间,发现很多人用其他东西来代替“json”,但我找不到其他人有这个问题。在下面你会找到我要的罐头。如果您还需要其他代码片段,请告诉我

这是我的Get组件:

import { Component, OnInit, ViewChild } from '@angular/core';
import { CorrectionCodeService } from '../Service/correctioncode.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ModalComponent } from 'ng2-bs3-modal/ng2-bs3-modal';
import { ICorrectionCode } from '../Models/ICorrectionCode';
import { DBOperation } from '../Shared/enum';
import { Observable } from 'rxjs/Rx';
import { Global } from '../Shared/global';

@Component({

templateUrl: 'app/Components/correctioncode.component.html'

})

`enter code here`export class CorrectionCodeComponent implements OnInit {

@ViewChild('modal') modal: ModalComponent;
corrCodes: ICorrectionCode[];
corrCode: ICorrectionCode;
msg: string;
indLoading: boolean = false;
form: FormGroup;
dbops: DBOperation;
modalTitle: string;
modalBtnTitle: string;

constructor(private fb: FormBuilder,private _corrCodeService: CorrectionCodeService) { }



ngOnInit() {
    this.form = this.fb.group({
        id: [''],
        pgm_code_name: [''],
        rec_act_code: [''],
        rgln_Year: [''],
        rgln_type_name: [''],
        rgln_one_code: [''],
        rgln_two_code: [''],
        rgln_three_code: [''],
        rgln_title_text: [''],
        rgln_desc_text: [''],
        sevrty_code: [''],
        valdtn_value: [''],
        last_updt_dt: [''],
        last_updt_logn_id: [''],
        creat_dt: [''],
        creat_logn_id: [''],
        del_dt: [''],
        del_logn_id: [''],
        prev_rec_id: ['']
    });

    this.getCorrectionCodes();

} 
 getCorrectionCodes(): void {
    this.indLoading = true;
    this._corrCodeService.get(Global.BASE_CORRECTION_CODE_ENDPOINT)
        .subscribe(corrCodes => {
            this.corrCodes = corrCodes; this.indLoading = false;
        }, error => this.msg = <any>error);
}
这是我的控制器:

using CorrectionCodeEditor.Models;
using CorrectionCodeEditor.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;


namespace CorrectionCodeEditor.Controllers
{
    public class CorrectionCodeController : BaseAPIController
    {
        string ProgramAreaName = "FAS";
        private CorrectionCodeRepository context;
        public CorrectionCodeController()
        {

            context = new CorrectionCodeRepository();
        }

        [HttpGet]
        public HttpResponseMessage Get()
        {
            var corrCodes = context.GetAllCorrectionCodes(ProgramAreaName).AsEnumerable();
                return ToJson(corrCodes);
        }
我的ToJson方法:

protected HttpResponseMessage ToJson(dynamic obj)
        {
            var response = Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
            return response;
        }
和我的上下文数据库存储库:

 public IEnumerable<CorrectionCode> GetAllCorrectionCodes(string pgm_area_name)
        {
            using (con)
            {
                 con.Open();
                var dP = new DynamicParameters();
                dP.Add("@ProgramAreaName", pgm_area_name);
                IEnumerable<CorrectionCode> corrCodeList = con.Query<CorrectionCode>("[mobile].[p_get_correction_codes_by_params]", dP, commandType: CommandType.StoredProcedure);
                return corrCodeList;
            }
        }
public IEnumerable GetAllCorrectionCodes(字符串pgm\u区域\u名称)
{
使用(con)
{
con.Open();
var dP=新的动态参数();
dP.添加(“@ProgramAreaName”,pgm\u区域\u名称);
IEnumerable corrCodeList=con.Query(“[mobile].[p_get_correction_codes_by_params]”,dP,commandType:commandType.storedProcess);
返回corrCodeList;
}
}
当屏幕显示“TypeError:Error.json不是函数”时,从Google Developer Tools编辑#1 Compile Tile Error:

SyntaxError:JSON中位置0处的意外标记<

在JSON.parse(

中,我强烈建议您使用Http客户端而不是Http,因为第一个客户端已被弃用。要使用它,请简单:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class AccountService {

  constructor(private http: HttpClient) {
   ...
  }

当然,您也应该在app.module中声明HttpClient,声明形式与Http相同。

结果表明@joshvito的角度正确,只是不完全正确。问题是,在我下面的教程中,建议我将RouteConfig.ts编辑为:

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
           routes.MapRoute(
                            name: "Default",
                            url: "{*anything}",
                            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                        );
        }
但该示例使用的是Web Api而不是ASP.net,而且由于我对Angular非常陌生,我不知道我应该编辑WebApiConfig.cs,它需要一个我没有传入的操作名称。因此,当我将组件中的get方法更改为以下内容时:

getCorrectionCodes(): void {
        this.indLoading = true;
        this._corrCodeService.get(Global.BASE_CORRECTION_CODE_ENDPOINT+"get")
            .subscribe(corrCodes => {
                this.corrCodes = corrCodes; this.indLoading = false;
            }, error => this.msg = <any>error);
    }
getCorrectionCodes():void{
this.indLoading=true;
这是.\u corrCodeService.get(Global.BASE\u CORRECTION\u CODE\u ENDPOINT+“get”)
.订阅(corrCodes=>{
this.corrCodes=corrCodes;this.indload=false;
},error=>this.msg=error);
}

一切都开始正常工作。

您不使用HttpClient而不是Http的具体原因是什么?这样就不需要在
.map((响应:响应)上调用
.json
)=>@joshvito不确定如何获得HTTPClient而不是Http。我是Angular 2引用风格的新手。你能解释一下我将在package.json和systemjs.config.js
map((response:response)=>response.json()中放置什么吗
你能这样试试吗?@tCoe,我喜欢你angular doc,它指导你设置新的HttpClient。按照这个指南,上面发布的angular代码对我来说很好。我没有看到任何明显的错误。你是否使用带有ts lint的IDE,可能有一个不太明显的类型错误?我为你投票了这个问题。也许下一次调试第一步是尝试在JSFIDLE或类似工具上添加一个指向可重现错误的链接。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class AccountService {

  constructor(private http: HttpClient) {
   ...
  }
 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
           routes.MapRoute(
                            name: "Default",
                            url: "{*anything}",
                            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                        );
        }
getCorrectionCodes(): void {
        this.indLoading = true;
        this._corrCodeService.get(Global.BASE_CORRECTION_CODE_ENDPOINT+"get")
            .subscribe(corrCodes => {
                this.corrCodes = corrCodes; this.indLoading = false;
            }, error => this.msg = <any>error);
    }