Angular 2 post method to pass two parameter

Asked
Active3 hr before
Viewed126 times

5 Answers

methodangularparameter
90%

We can pass to the get function a set of optional query parameters as the second argument,,To perform a DELETE request we just call the delete function

Example_snippet/controller/utility/_method.js/ class MyClass { constructor(. . .
class MyClass {
   constructor(private http: Http) {}
}
load more v
88%

Web API has no issue allowing for single parameter like this:,If you have both complex and simple parameters, you can pass simple parameters on the query string, The above would actually work with:,Which is the best way to handle a post to the webapi passing multiple parameters where one of them is a file?,Awesome post, Accessing post parameters with JObject resolved my issue

Example_snippet/controller/utility/_method.js/ RouteTable.Routes.MapHttpRoute. . .
RouteTable.Routes.MapHttpRoute(
   name: "AlbumRpcApiAction",
   routeTemplate: "albums/**{action}**/{title}",
   defaults: new {
      title = RouteParameter.Optional,
         controller = "AlbumApi", **
         action = ** ** "GetAblums" **
   }
);
Step 2 continued with RouteTable.Routes.MapHttpRoute. . .
RouteTable.Routes.MapHttpRoute(
   name: "AlbumRpcApiAction",
   routeTemplate: "albums/**{action}**/{title}",
   defaults: new {
      title = RouteParameter.Optional,
         controller = "AlbumApi", **
         action = ** ** "GetAblums" **
   }
);
Step 3 continued with $.ajax( { url: "albums/Pos. . .
$.ajax({
   url: "albums/PostAlbum",
   **
   type: "POST",
   **
   **
   data: {
      AlbumName: "Dirty Deeds",
      Entered: "5/1/2012"
   },
   **
   success: function(result) {
      alert(result);
   },
   error: function(xhr, status, p3, p4) {
      var err = "Error " + " " + status + " " + p3;
      if (xhr.responseText && xhr.responseText[0] == "{")
         err = JSON.parse(xhr.responseText).message;
      alert(err);
   }
});
Step 4 continued with album = { AlbumName: "Powe. . .
album = {
   AlbumName: "PowerAge",
   Entered: new Date(1977, 0, 1)
}
$.ajax({
   url: "albums/PostAlbum",
   type: "POST",
   **
   contentType: "application/json",
   **
   ** data: JSON.stringify(album),
   **
   success: function(result) {
      alert(result);
   }
});
Step 5 continued with [HttpPost] public string PostA. . .
[HttpPost]
public string PostAlbum(Album album, string userToken)
Step 6 continued with public PostAlbumResponse PostA. . .
public PostAlbumResponse PostAlbum(PostAlbumRequest request) {
   var album = request.Album;
   var userToken = request.UserToken;

   return new PostAlbumResponse() {
      IsSuccess = true,
         Result = String.Format("{0} {1:d} {2}", album.AlbumName, album.Entered, userToken)
   };
}
Step 7 continued with public class PostAlbumRequest . . .
public class PostAlbumRequest {
   public Album Album {
      get;
      set;
   }
   public User User {
      get;
      set;
   }
   public string UserToken {
      get;
      set;
   }
}

public class PostAlbumResponse {
   public string Result {
      get;
      set;
   }
   public bool IsSuccess {
      get;
      set;
   }
   public string ErrorMessage {
      get;
      set;
   }
}
Step 8 continued with var album = { AlbumName: ". . .
var album = {
   AlbumName: "PowerAge",
   Entered: "1/1/1977"
}
var user = {
   Name: "Rick"
}
var userToken = "sekkritt";

$.ajax({
   url: "samples/PostAlbum",
   type: "POST",
   contentType: "application/json",
   **
   data: JSON.stringify({
      Album: album,
      User: user,
      UserToken: userToken
   }),
   **
   success: function(result) {
      alert(result.Result);
   }
});
Step 9 continued with var album = { AlbumName: ". . .
var album = {
   AlbumName: "PowerAge",
   Entered: "1/1/1977"
}
var user = {
   Name: "Rick"
}
var userToken = "sekkritt";

$.ajax({
   url: "samples/PostAlbum",
   type: "POST",
   contentType: "application/json",
   **
   data: JSON.stringify({
      Album: album,
      User: user,
      UserToken: userToken
   }),
   **
   success: function(result) {
      alert(result.Result);
   }
});
Step 10 continued with var album = { AlbumName: ". . .
var album = {
   AlbumName: "PowerAge",
   Entered: "1/1/1977"
}
var user = {
   Name: "Rick"
}
var userToken = "sekkritt";

$.ajax({
   url: "samples/PostAlbum",
   **
   type: "POST",
   contentType: "application/json",
   **
   data: JSON.stringify({
      Album: album,
      User: user,
      UserToken: userToken
   }),
   success: function(result) {
      alert( ** result ** );
   }
});
load more v
72%

string | null: The first value of the given parameter, or null if the parameter is not present,,An HTTP request/response body that represents serialized parameters, per the MIME type application/x-www-form-urlencoded

Example_snippet/controller/utility/_method.js/ class HttpParams . . .
      
      class HttpParams {
  constructor(options: HttpParamsOptions = {} as HttpParamsOptions)
  has(param: string): boolean
  get(param: string): string | null
  getAll(param: string): string[] | null
  keys(): string[]
  append(param: string, value: string | number | boolean): HttpParams
  appendAll(params: { [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>; }): HttpParams
  set(param: string, value: string | number | boolean): HttpParams
  delete(param: string, value?: string | number | boolean): HttpParams
  toString(): string
}
    
load more v
65%

you need to crate a class to pass multiple params to a post, it is in the definition of the post method, if you want to pass standalone parameters,,I want to post data on an action which has two parameter in it ho could I achieve

Example_snippet/controller/utility/_method.js/ import { Http, Headers } from . . .
import {
   Http,
   Headers
} from 'angular2/http';
Step 2 continued with let url = 'base api url/Accoun. . .
let url = 'base api url/Account/Login';
let data = {
   "returnUrl": this.returnUrl,
   "email": this.email
};
let body = JSON.stringify(data)
let head = new Headers({
   'Content-Type': 'application/json'
});

this.http.post(url, body, {
      headers: head
   })
   .map(res => res.json())
   .subscribe((data: any) => {
      //subscribe actions
   });
load more v
75%

Example_snippet/controller/utility/_method.js/ getLogs(logNamespace): Observa. . .
getLogs(logNamespace): Observable<any> {

    // Setup log namespace query parameter
    let params = new HttpParams().set('logNamespace', logNamespace);

    return this._HttpClient.get(`${API_URL}/api/v1/data/logs`, { params: params })
}
load more v