0

I don't understand how should I make post method in javascrpt for example:

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
  \"name\": \"GreatNight\",
  \"city\": \"Tehran\"
}" "http://localhost:3000/api/CoffeeShops"

how could be looks like this code in javascript?

RSA
  • 1,417
  • 4
  • 22
  • 37
  • 2
    cURL isn't available in Javascript. If you're talking client-side (browser) JS then what you want is an AJAX request. You can google hundreds of examples of how to do it. If you're talking node.js then see here: https://stackoverflow.com/questions/6819143/curl-equivalent-in-nodejs . P.S. not sure what you are referring to by "loopback" in this context? – ADyson Jul 26 '17 at 11:58
  • @adyson ,thanks. I want to use crud api in mobile app, which is implementing b, ionic and uses typescript/ javascript and angular. – RSA Jul 26 '17 at 12:43
  • You may Need to know more about node.js https://www.npmjs.com/package/curl. – Parsaria Jul 26 '17 at 13:39
  • @Parsaria that package is rather cheekily called "curl" but it doesn't actually use or re-implement the original cURL. It just wraps up node's "request" package (https://www.npmjs.com/package/request) in some shorthand methods. Also OP has already mentioned they aren't using node.js here. – ADyson Jul 26 '17 at 15:44
  • So how is to implement @Reza its code? Ionic uses angular may be this is helpful . https://angular.io/api/http/Http – Parsaria Jul 26 '17 at 17:49

2 Answers2

1

with jQuery ajax

$.ajax({
  type: 'POST',
  url: "http://localhost:3000/api/CoffeeShops",
  data: {
    name: "GreatNight",
    city: "Tehran"
  },
  dataType: "json",
  accepts: {
    text: "application/json"
  }
});
ewwink
  • 18,382
  • 2
  • 44
  • 54
  • Thanks but I don't think that jquery and Ajax will solve my problem in Ionic App. – RSA Jul 26 '17 at 14:58
1

this is the solution:

html

<ion-content>
  <ion-list>
    <ion-item>
      <ion-label fixed> shop name </ion-label>
      <ion-input type="text"[(ngModel)]="name" > </ion-input>
    </ion-item>

    <ion-item>
      <ion-label fixed> city </ion-label>
      <ion-input type="text" [(ngModel)]="city"> </ion-input>
    </ion-item>

    <ion-item>
      <button ion-button block large (click)="post()">post  </button>
    </ion-item>

  </ion-list>

</ion-content>

page2.ts

import { Component } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';

import { NavController, NavParams } from 'ionic-angular';

@Component({
  selector: 'page-page2',
  templateUrl: 'page2.html'
})
export class Page2 {
  public name;
  public city;
  constructor( public navCtrl: NavController, public navParams: NavParams, public http: Http) {   }

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

let data = JSON.stringify({
  name:this.name,
  city:this.city
});
console.log(data);
let url = 'http://localhost:3000/api/CoffeeShops';
return new Promise((resolve, reject) => {
  this.http.post(url, data, options)
  .toPromise()
  .then((response) =>
  {
    console.log('API Response : ', response.json());
    resolve(response.json());
  })
  .catch((error) =>
  {
    console.error('API Error : ', error.status);
    console.error('API Error : ', JSON.stringify(error));
    reject(error.json());
  });
});
}

}
RSA
  • 1,417
  • 4
  • 22
  • 37