AngularJs - 服务器抛出异常“XMLHttpRequest 无法加载“URL";预检响应无效(重定向)' [英] AngularJs - Server throwing exception''XMLHttpRequest cannot load the "URL" Response for preflight is invalid (redirect)'

查看:47
本文介绍了AngularJs - 服务器抛出异常“XMLHttpRequest 无法加载“URL";预检响应无效(重定向)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器将对象返回为未定义"和XMLHttpRequest 无法加载URL"的原因是什么,预检响应无效(重定向)..>

应用流程 - 它只是一个普通的邮政服务,向服务器发送文档详细信息作为回报应该返回一个包含各种参数的对象,但它返回未定义"

发布文档的服务

文件上传:{方法:'POST',url: config.apiPath + 'employee/service/pushRecords',//这是应该返回不同参数集的对象的URL(目前它返回的错误错误[未定义])isArray:假,参数:{},标题:{内容类型":未定义}},

我在创建 formdata w.r.t 文档后使用的上述服务

function registerFormdata(files, fieldName) {文件 = 文件 ||[];字段名称 = 字段名称 ||'文件已发送';var returnData = new FormData();_.each(files, function (file, ind) {returnData.append(fieldName,file);});返回返回数据;}

现在这是使用这些服务的控制器

function sendFilesToServer() {var formData = employeePushService.registerFormdata(directive.dropZoneFile.fileToUpload);返回 docUploadService.fileUpload(formData).then(功能(文​​档){//额外的操作,但这里服务器应该返回一个带有一组参数的对象,但在浏览器控制台中它的错误 [未定义]}).catch(logger.error);}

解决方案

假设你帖子中的 URL target 是正确的,看来你的 CORS 有问题,让我解释一下.不知道是不是自己开发的服务端API,如果是,需要加上CORS访问,你的服务端必须返回这个header:

<块引用>

访问控制允许来源:http://foo.example

您可以将 http://foo.example 替换为 *,这意味着所有请求源都可以访问.

首先要知道,在客户端发出AJAX CORS请求时,浏览器首先向服务器做一个请求,检查服务器是否允许该请求,这个请求是一个OPTION方法,可以看这个例如,如果在 chrome 中启用开发工具,则在网络选项卡中,您可以看到该请求.因此,在该 OPTIONS 请求中,服务器必须在响应标头中设置 Access-Control-Allow-Origin 标头.

因此,您必须检查此步骤,您的问题是服务器端不允许您的请求.

顺便说一句,CORS 请求中并非支持所有内容类型,此处 您有更多信息肯定会有所帮助.

另一个链接有助于解决问题由于重定向而发生 302.在这种情况下,POST 响应还必须包含 Access-Control-Allow-Origin 标头.

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);
    }

解决方案

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.

这篇关于AngularJs - 服务器抛出异常“XMLHttpRequest 无法加载“URL";预检响应无效(重定向)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆