2

What is the reason the server is returning object as 'undefined' and 'XMLHttpRequest cannot load the "URL" Response for preflight is invalid (redirect).

Flow of app - its just a normal post service sending document details to the server in return should return an object holding various parameters, but its returning 'undefined'

The service for posting the document

fileUpload: {
    method: 'POST',
    url: config.apiPath + 'employee/service/pushRecords', //this is the URL that should return an object with different set of parameters (currently its returning Error error [undefined])
    isArray: false,
    params: {},
    headers: {
        'content-type': undefined
    }
},

above service i have used after creating formdata w.r.t document

function registerFormdata(files, fieldName) {
    files = files || [];
    fieldName = fieldName || 'FileSent';
    var returnData = new FormData();

    _.each(files, function (file, ind) {
        returnData.append(fieldName,file);
    });

    return returnData;
}

now this is the controller where these services are used

function sendFilesToServer() {
        var formData = employeePushService.registerFormdata(directive.dropZoneFile.fileToUpload);
        return docUploadService.fileUpload(formData)
            .then(function(document) {              
         // Extra actions but here the server should be returning an object with set of parameters but in browser console its Error [undefined]
            }).catch(logger.error);
    }
Kalamarico
  • 5,466
  • 22
  • 53
  • 70

1 Answers1

0

Assuming that the URL target in yout post is correct, it seems that you have a CORS problem, let me explain some things. I don't know if the server side API it's developed by yourself, if it is, you need to add the CORS access, your server must return this header:

Access-Control-Allow-Origin: http://foo.example

You can replace http://foo.example by *, it means that all request origin will have access.

First, you need to know that when in the client you make an AJAX CORS request, your browser first do a request to the server to check if the server allow the request, this request is a OPTION method, you can see this if, for example in chrome, you enable the dev tools, there, in the network tab you can see that request. So, in that OPTIONS request, the server must set in the response headers, the Access-Control-Allow-Origin header.

So, you must check this steps, your problem is that the server side is not allowing your request.

By the way, not all the content-type are supported in CORS request, here you have more information that sure will be helpfull.

Another link to be helpfull for the problem when a 302 happens due to a redirect. In that case, the POST response must also include the Access-Control-Allow-Origin header.

Kalamarico
  • 5,466
  • 22
  • 53
  • 70