Using xhr.getResponseHeader to get Total # of Records

Using xhr.getResponseHeader to get Total # of Records

timcadieuxtimcadieux Posts: 76Questions: 22Answers: 0

Using serverside to return content from an api. The Total RecordCount is returned in the Response header..
you can see here

Attemptinng to use xhr.getResponseHeader('total'); but it's always null..i must be missing something super simple.

ideas?

BIN

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    The Total RecordCount is returned in the Response header..

    I'm not seeing that. The only response returned in your fiddle is the data array. The protocol is discussed here.

    Cheers,

    Colin

  • timcadieuxtimcadieux Posts: 76Questions: 22Answers: 0

    It seems like you're telling me to change how the content is returned to me, I cannot.

    If you load the Bin in Chrome, use F12, goto Network Tab, XHR, Refresh, click on the List call and you will see the total of 199 returned in the Response Headers.

    Just trying to access it.

  • kthorngrenkthorngren Posts: 21,447Questions: 26Answers: 4,974

    It seems like the only response header in the xhr object is conten-header accroding to the getAllResponseHeaders(). Not sure why though:
    http://live.datatables.net/ligoroba/2/edit

    Hopefully @allan or @colin can tell us why.

    Kevin

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    Answer ✓

    I believe this is a security protection by the browser since the access-control-expose-headers property is not being set by the server. You need to have the server include:

    access-control-expose-headers: total
    

    for it to work.

    With that done, Kevin's solution should work.

    Allan

  • timcadieuxtimcadieux Posts: 76Questions: 22Answers: 0

    Yeah, I already have this in my .netCore startup but I can't read the total.

    I've created a .netCore Cors related StackOverflow question to try and figure it out.

    services.AddCors(options => options.AddPolicy("ApiCorsPolicy", build => build
    .AllowAnyOrigin()
    .AllowAnyHeader()
    .AllowAnyMethod()
    .AllowCredentials()));

This discussion has been closed.