0

In my app, I am making a http.post request and getting back a value of 'gradeS'. Now I wanted to use this value of gradeS as parameter to make another cascaded call and fetch some list of names. I'll post the code I have written so far --

 1. my first call [originally]

            this.http.post('api/SampleData/CustomerGradeByID', JSON.stringify(body), { headers: _Headers })
            .subscribe((result: any) =>
            {
                this.gradeS = result.json() as any[]
            },
            (error: any)=>console.error(error))


 2. my service function

    export class CommListService {
    endPoint: string;

    constructor(private http: Http) {
        this.endPoint = "/api/SampleData/MakeCommunicationList";
    }

    GetfacilityNames(grade: string): Observable<Communication[]> {
        var body = grade;

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

        var commList = this.http.post(this.endPoint, JSON.stringify(body))
            .map((r: Response) => {
                return (r.json().length != 0 ? r.json() : [{"facility_name": "No Record Found" }]) as Communication[]
            });
        return commList;
    }
}

 3. my model class

    export interface ICommunication {
        facility_name: string;
    }

    export class Communication implements ICommunication {
        facility_name: string;
    }

 4. how I tried to merge

 this.http.post('api/SampleData/CustomerGradeByID', JSON.stringify(body), { headers: _Headers })
        .do(u => this.gradeS = u)
        .flatMap(u => this.commu.GetfacilityNames(this.gradeS))
        .subscribe(p => this.comms = p);


 5. finally in my html 

     <ul>
         <li *ngFor="let cm of comms">{{cm.facility_name}}</li>
     </ul>

SO where did I go wrong? I am getting data from database. By running a stored procedure. Whats' with the 415 status? What does it mean in this context? Could there be something wrong in the server side C# code? I have a hunch that I could have written something wrong at the server side stuff the backend comprising of C# and MSSQL. Although I am not sure.

this is my C# code

public JsonResult MakeCommunicationList([FromBody]string grade)
        {
            var gId = _appDbContext.CustomerGrades.First(m => m.grade_name == grade);
            try
            {
                var inpParam = new SqlParameter("gId", SqlDbType.BigInt) { Direction = ParameterDirection.Input };

                var salesData = (_appDbContext.Database.ExecuteSqlCommand($"EXEC {"sp_GetCommunicationList"} @gId", inpParam));
                return Json(salesData);
            }
            catch (Exception exp)
            {
                return Json(exp.GetBaseException());
            }
        }

and this is my SP

CREATE procedure [dbo].[sp_GetCommunicationList]

@gId bigint

as
begin

begin try

    if exists (
            Select * from tempdb..sysobjects Where id = object_id('tempdb.dbo.#temp')
          )
        DROP TABLE #temp

else
        select facility_name
        into #temp 
        from Facilities ff
        join
        GradeFacilities gf
        on
        gf.facility_id=ff.Id
        where
        grade_id=@gId

--      select * into #temp from Orders where customer_id=@customer_id
end try

begin catch
    select error_message(),
           error_severity(),
           error_number(),
           error_state()
end catch

end

Please Help!! I am unable to figure this out!

Prabir Choudhury
  • 143
  • 1
  • 18
  • I'm pretty sure the `415` status code (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415) is because you are sending the data incorrectly in your request. Can you give details around what your `body` variable looks like so that we can give you further advice? If it is just a string, I don't think you are sending enough for .NET to populate the input parameter as your payload will simple be a string with no relation to the parameter it is for. – Daniel W Strimpel Sep 24 '18 at 19:07
  • Depending on how your server is set up, you'll need the post to be formatted like a query string would (i.e. `grade=INSERT_GRADE_HERE`) or in JSON format (i.e. `{ "grade": "INSERT_GRADE_HERE" }`). You can use Chrome's dev tools (or dev tools in your browser of choice) to inspect the network traffic to see what your post is formatted like. – Daniel W Strimpel Sep 24 '18 at 19:19
  • could be @DanielWStrimpel becuase that's what I am trying to learn - using the merge or flatmap or whatever how to send the data reeived from the first call 'gradeS' as argument to the geFacilityNames function in the service call. My body variable is quite simple. string like D, E, A, A+ etc.... – Prabir Choudhury Sep 25 '18 at 13:57
  • I am not sure how to do this... – Prabir Choudhury Sep 25 '18 at 14:00
  • See if this SO post helps you with figuring out how to set up your body. https://stackoverflow.com/questions/35212341/angular2-http-post-request-parameters – Daniel W Strimpel Sep 26 '18 at 16:04

0 Answers0