对于iteminfo的Dojo.request - 在Fiddler中返回代码200,但在浏览器中返回0

时间:2016-01-06 11:58:02

标签: javascript json dojo arcgis-js-api

我使用dojo.request中的iteminfo代码与a previous project中使用相同的代码,但URL地址和我称之为服务addFeatures而不是图层Unable to load http://foobar.com/arcgis/rest/services/MapServer/info/iteminfo status: 0 的事实除外。但是,我总是收到以下错误:

var promise = require('dojo/request').post(uri, {
    handleAs: "json",
    query: "f=pjson",        
    headers: {
        "X-Requested-With": null
    },
    //timeout: 60000,  //commenting this in and out changes nothing
    withCredentials: true
});

var res = promise.isResolved();
var rej = promise.isRejected();
var ful = promise.isFulfilled();
var can = promise.isCanceled();
var respres = promise.response.isResolved();
var resprej = promise.response.isRejected();
var respful = promise.response.isFulfilled();
var respcan = promise.response.isCanceled();

promise.response.then(
  //success
  function (response) {
    //something
  },
  //fail
  function (error) {
    //something different
  }
);

我在Firefox和IE中证实了这一点。 Firebug或Visual Studio显示的状态始终为0,即使Fiddler中显示的状态为200.当我弄乱了某些东西并在Fiddler中获得401时,它也显示为0。

所以,这是我的代码:

query

我希望输出为JSON,所以我添加了timeout选项;没有它,Fiddler将输出显示为HTML。我发现answer表示响应可能需要时间,但要么不是我的情况,要么比简单的res选项更复杂。

我的所有测试变量(rejfalse ...)都会保留accept。因此,代码中的请求似乎没有得到解决,未完成等等,但Fiddler返回响应 - 我对这些网络艺术不够熟练,无法从中得出结论,所以我问这个问题。

另外,我有一种模糊的印象,即一些额外的标题应该是解决方案。我尝试添加content-type(没有更改)和Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept Access-Control-Allow-Origin: http://localhost:30322 //origin for now Access-Control-Allow-Origin: * (它已经pre-flight而Fiddler报告状态为401)。

编辑:响应标头包含以下安全信息:

public IEnumerable<Navbar> adminMenuItems()
    {
        var menu = new List<Navbar>();
        menu.Add(new Navbar { Id = 1, nameOption = "Dashboard", controller = "Home", action = "DashBoard", imageClass = "fa fa-dashboard fa-fw", status = true, isParent = false, parentId = 0 });
        menu.Add(new Navbar { Id = 2, nameOption = "Maintenance", controller = "MaintenancePlan", action = "Index", imageClass = "fa fa-wrench fa-fw", status = true, isParent = true, parentId = 0 });
        menu.Add(new Navbar { Id = 3, nameOption = "Schedules", controller = "WorkOrder", action = "Index", imageClass = "fa fa-briefcase fa-fw", status = true, isParent = false, parentId = 2 });

        menu.Add(new Navbar { Id = 4, nameOption = "Work Orders", controller = "WorkOrder", action = "Index", imageClass = "fa fa-briefcase fa-fw", status = true, isParent = true, parentId = 2 });
        menu.Add(new Navbar { Id = 5, nameOption = "Assigned", controller = "WorkOrder", action = "Index", imageClass = "fa fa-briefcase fa-fw", status = true, isParent = false, parentId = 4 });
        menu.Add(new Navbar { Id = 6, nameOption = "Company", controller = "WorkOrder", action = "Index", imageClass = "fa fa-briefcase fa-fw", status = true, isParent = false, parentId = 4 });

        menu.Add(new Navbar { Id = 7, nameOption = "Calender", controller = "WorkOrder", action = "Index", imageClass = "fa fa-briefcase fa-fw", status = true, isParent = false, parentId = 2 });
        menu.Add(new Navbar { Id = 8, nameOption = "Quotations", controller = "WorkOrder", action = "Index", imageClass = "fa fa-briefcase fa-fw", status = true, isParent = false, parentId = 2 });
        menu.Add(new Navbar { Id = 9, nameOption = "Assets", controller = "WorkOrder", action = "Index", imageClass = "fa fa-briefcase fa-fw", status = true, isParent = false, parentId = 2 });
        menu.Add(new Navbar { Id = 10, nameOption = "Site", controller = "WorkOrder", action = "Index", imageClass = "fa fa-briefcase fa-fw", status = true, isParent = false, parentId = 2 });

        return menu.ToList();
    }

2 个答案:

答案 0 :(得分:1)

浏览器越来越严重(出于安全原因)关于发出跨域请求,并且可能只是拒绝加载跨域资源,即使它的状态为200,而是显示为0.所以如果域名在您的dojo请求中使用的uri与您网站uri中的域名不同,这可能是问题所在。

请注意,只需添加符合条件的端口号具有不同的域,并且将被视为跨域请求。

要解决此问题,您可以在您的域上发出所有请求,并在您的dojo请求中添加内容,以使您的Web服务器代理对外部服务器的请求,例如:

    RewriteRule /arcgis/(.*)  http://foobar.com/arcgis/$1 [L,P]
    ProxyPassReverse /arcgis http://foobar.com/arcgis

否则,您必须手动指定来自您网站域的跨域请求正常。 foob​​ar.com服务器需要将HTTP标头Access-Control-Allow-Origin与您的域值一起添加到响应中,您的浏览器将接受它。添加值为*的标头Access-Control-Allow-Origin将接受来自任何域的跨域请求,但显然不太安全。

答案 1 :(得分:0)

我试图预检请求。发送的OPTIONS请求似乎没问题,但我们的服务器需要对它进行身份验证,导致错误401.我不想尝试hack the OPTIONS request,所以这只能在服务器端解决。由于所有这些的目的只是在测试和生产中,所有内容都将在该服务器上或代理后面,我们决定使用一些肮脏的解决方法,例如暂时将JSON缓存在文件中。

对于为什么Access-Control-Allow-Origin还不够的谜团,我认为POST可能是罪魁祸首。它与JSON-P和简单的CORS请求都不兼容。另外,我从未获得Access-Control-Allow-Methods响应标头,因此他们的缺乏可能会阻止请求。我虽然不确定。