Exclude Odata metadata and type from JSON

2020-05-08 02:06发布

问题:

The script below return a JSON from my ODataController

< script >
  $(document).ready(function() {
    $.ajax({
      url: "http://localhost:37994/odata/EPStructures3/",
      type: "Get",
      contentType: 'application/json; charset=utf-8',
      success: function(data) 
      {
        //do something.....
      },
      error: function(msg)
      {
        alert(msg);
      }
    });
  }); 
< /script>

The JSON :

{ "odata.metadata":"http://localhost:37994/odata/$metadata#EPStructures3","value":[{"eps_level":0,"id":2},{"eps_level":1,"id":3}]}

I want to exclude the metadata and type so the JSON is returned like this:

[{"eps_level":0,"id":2},{"eps_level":1,"id":3}]

How can I achieve this?

回答1:

If you add an "Accept: application/json; odata.metadata=none" header to your request you will get as close as you can to what you want.