0

I want to send 2 values from angular to spring boot API, in spring boot API there is a query which is accepting some parameter which I have hardcoded, but want to send that parameter from angular in that query So as given in query there are two parameters, so want to post this parameter to API from angular and get data in angular according to that parameter passed,

this is the daoimpl in which query has 2 arguments

@Override
public List<ThingworxStoreDeviceConnStatusBean> getStoreDS() {
    System.out.println("ThingworxDaoImp is runing" + "\\getStoreDS");
    String Query = "select * from ncr_store_connectivity_info('28152','0002')";
    System.out.println("Query getAllStores::" + Query);
    List<ThingworxStoreDeviceConnStatusBean> thingworxModelList = jdbcTemplate.query(Query, new RowMapper<ThingworxStoreDeviceConnStatusBean>() {
        @Override public ThingworxStoreDeviceConnStatusBean mapRow(ResultSet rs, int rowNum)throws SQLException {
            ThingworxStoreDeviceConnStatusBean mThingworxStoreDeviceConnStatusBean = new ThingworxStoreDeviceConnStatusBean();
            mThingworxStoreDeviceConnStatusBean.setConnected(rs.getInt("connected"));
            mThingworxStoreDeviceConnStatusBean.setDisconnected(rs.getInt("disConnected"));
            mThingworxStoreDeviceConnStatusBean.setTotal(rs.getInt("total"));
            mThingworxStoreDeviceConnStatusBean.setConnected_per(rs.getString("connected_per"));
            return mThingworxStoreDeviceConnStatusBean;
        }
    });
    return thingworxModelList;
}

and this fronthand code where I am getting data from API

getTotalDevice() {
    this.users2 = this
        ._dataService
        .getCustomerInfo()
        .subscribe((result2) => {
            this.users2 = result2
            console.log("total subset1" + result2);
            // LOGIC FOR SPLITING ARRAY
            for (let i = 0; i < this.users2.length; i++) {
                this.Jsonobj3 = this.users2[i];
                console.log("json " + JSON.stringify(this.Jsonobj3));
                this.Connect_Total = this.Jsonobj3['connected'];
                console.log("inside loop connected" + this.Connect_Total[i]);
                this.DisConnect_Total = this.Jsonobj3['disconnected'];
                this.totalSubset = this.Jsonobj3['total'];
                this.Connected_per = this.Jsonobj3['connected_per'];
                this.Discoonected_per = 100 - this.Connected_per;
            }
            console.log("result array" + this.DisConnect_Total);
            console.log("connected total" + this.Connect_Total);
            console.log("total subset" + this.totalSubset);
            console.log("Connected_per" + this.Connected_per);
            console.log("disconnecetd per" + this.Discoonected_per)
            this.ConnectMethod(this.Connect_Total);
            // this.options.series[0]['data']=this.Connect_Total
            Highcharts.chart('card', this.options);
            Highcharts.chart('card', this.options1);
            // console.log(result2)
            // if (result2) {
            // this.user2 = result2;
            // console.log(this.user2[0].ncr_customer_info.split(','));
            // var splittedArray = this.user2[0].ncr_customer_info.split(',');
            // for (var i = 0; i < splittedArray.length; i++) {
            //     this.globalObj[i] = splittedArray[0].value;
            // }
            // console.log("connected" +this.globalObj);
            // }
        }, (error) => {})

1 Answer 1

0

You can Post Is HttpClient this

getData(modalClass: YourModalClass): Observable<YourModalClass> {
      return this.http.post<YourModalClass>("Your Url", modalClass, httpOptions)
        .pipe(
          catchError(this.handleError('addHero', hero))
        );
    }

2nd method if string

If you are using something like json-server

import { Http, Headers, RequestOptions } from '@angular/http';

const headers = new Headers({'Content-Type': 'application/json'});
const options = new RequestOptions({headers: headers});

this.http.post(this.url, JSON.stringfy(data), options)...
Sign up to request clarification or add additional context in comments.

3 Comments

how to send only two variable that are string to api?
but showing Access to XMLHttpRequest at 'localhost:8080/getTotalSubset' from origin 'localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
... it's the CORS problem ... it is related to your server side configuration. It's not related to Angular at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.