简体   繁体   中英

AngularJS Factory : JSon and Authorization

I have a problem with my AngularJS factory. Actually, I have to make a request which includes a JSon variable, and it has an Authorization header.

It looks like this :

createEvent:function(event){
    return $http.get(url, {
        headers: { 'Authorization' : token }
    })
}

The event variable is a JSon object, and I want to get it in the $http.get function as I have a Validator in the backend which tests this json file. Is there any way for me to pass this variable here ?

Thanks !

You can pass event json as data.

createEvent:function(event){
    return $http.get(url, {
        headers: { 'Authorization' : token },
        data : event
    })
}

You can simply send the data using params object when using GET http method. Other thing is if you are sending JSON string you need to JSON.parse it.

...
$http.get(url, {
    headers: {'Authorization': token},
    params: event // or "JSON.parse(event)" if event is json string
});

It looks strange that you use the 'GET' method to send data.

createEvent:function(event){
  return $http.post(url, {
      headers: { 'Authorization' : token },
      data : event
  })
}

seems better.

Also check that your content type is correctly set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM