0

How do I add an accept header 'text/xml' in JQuery ajax file upload? It will upload a PDF file.

Here is the code I am using:

var fd = new FormData(document.getElementById('file'));
$.ajax({
  url: '/myurlhere',
  type: 'POST',
  data: fd,
  cache: false,
  contentType: false,
  processData: false,
  success: function(response) {
    console.log('success... '+response);
  }
}).fail(function(xhr,status,error) {
  console.log(error);
});

The server expects the accept header to be xml, so causes a 406 error. I have tried using dataType: 'xml' or header { Accept: 'text/xml' } and it causes a 400 error.

1 Answer 1

0

You have to set your contentType to "text/xml".

Change your call like this:

var fd = new FormData(document.getElementById('file'));
$.ajax({
  url: '/myurlhere',
  type: 'POST',
  data: fd,
  cache: false,
  contentType: "text/xml",
  dataType: "text",
  processData: false,
  success: function(response) {
    console.log('success... '+response);
  }
}).fail(function(xhr,status,error) {
  console.log(error);
});

see this SO answer it may help

jQuery ajax post to web service

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I have tried contentTYpe: 'text/xml' and dataType: 'text', but I am getting a 406 Not Acceptable error and I checked the developer tools and it shows the Accept header as text/plain, which is probably why it is not working because the server accepts text/xml. I tried dataType: 'xml' and I get 400 error. Any other ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.