0

Am very new to angularjs and i need to post data to a web service, the service accepts two parameters, one is List of object and the other is securityToken,

Here is my code,

$scope.saveCompany=function(){

      // alert("Save Company!!!");

     var Companies={
        Code: 'testMartin',
        Name: 'company1',
        CompanyType : 'Tenant',
        email : 'test@yaoo.com',
        Fax : 4235353,
        ParentID : 1

    };
    $http({
        url:'http://localhost/masters/smstools.svc/json/SaveComapnies',
        dataType: 'json',
        method: 'POST',
        data: $.param(Companies),
        headers: {
            "Content-Type": "text/json",
        }

    }).success(function(response){
        alert ("Success");
    }).error(function(error){
        alert ("Save company!");
    });

how can i pass the security token with the companies object as a separate paramenter. my service accepts the parameters like this,

 public List<CompanyProfile> Save(List<CompanyProfile> CompanyList,string securityToken)
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396

2 Answers2

0

Since this is a rest call you only have 3 places were you can pass parameters data:

With Post and it will be part of the body, it seems this is what is your first parameter is occupying now.

With Get and you add the parameter to the URL /json/SaveComapnies/mySecParam or by queryString like /json/SaveComapnies?sec=mySecParam but this is not secure nor recommended for security settings.

With header from angular Post:

    **headers: {
        "Content-Type": "text/json",
        "mySecVar": "mySecParamValue"
    }**

Server side version:

public List<CompanyProfile> Save(List<CompanyProfile> CompanyList){

    WebOperationContext current = WebOperationContext.Current;
    WebHeaderCollection headers = current.IncomingRequest.Headers;

    if (headers["mySecVar"] != null){
         // do something
    }
}

You can read more about it here:

How to read HTTP request headers in a WCF web service?

Community
  • 1
  • 1
Dalorzo
  • 19,834
  • 7
  • 55
  • 102
0

Can you share more information in your Backend?

If it is actually a REST Backend I would rather use an angular $resource

https://docs.angularjs.org/api/ngResource

If you want to pass json object and string as post Parameter you should stick to the $http docs

https://docs.angularjs.org/api/ng/service/$http

In the post example you can pass both params in:

$http.post('/yourEndpoint', {jsonObj:yourCompaniesObj, secKey:yourSecretToken})....(sucess etc)

Typing from my cell - if you need more code examples just tell

smartbart24
  • 504
  • 4
  • 4