Angular, content type is not being generated correctly when using resource

前端 未结 2 1681
慢半拍i
慢半拍i 2020-12-19 07:32

I have tried the following command, using resource on Angular:

angular.module(\'appServices\', [\'ngResource\']).facto         


        
相关标签:
2条回答
  • 2020-12-19 08:09

    Looks like it's still not supported - if you have to set the headers you can use the $http service

    0 讨论(0)
  • 2020-12-19 08:22

    Look at the angular source, line 8742 in the version 1.1.4:

      // strip content-type if data is undefined
      if (isUndefined(config.data)) {
        delete reqHeaders['Content-Type'];
      }
    

    The Content-Type header gets removed if the request does not contain any data (a request body).

    I think this is the expected behaviour since GET requests do not have a body.

    A POST method in the other hand, will set the content-type as you expect, as long it has data in the request body. Try the following:

    Change the method to POST

    query: {method:'POST', headers: {'Content-Type': 'application/json'}}
    

    And call your resource action with some parameter:

    Example.query(yourData)
    

    In this case the content type is correctly set.

    Edit:

    It seems it also works with get, in this case the data is in the second parameter:

    Example.query(yourParams, yourData)
    

    An example: http://jsfiddle.net/WkFHH/

    0 讨论(0)
提交回复
热议问题