本文介绍了Cross Origin Policy& Fiddler JSON调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用JSON数据的现代webapp。我在中找到了关于使用名为Fiddler的工具以模拟JSON数据。



我使用Notepad ++进行本地开发,主要在Chrome上进行测试(我最终将关注更多浏览器)。我有一个主要的HTML文件index.html,我使用的文件结构如下所示:

  index.html 

资产

/ js

/ css

/ img

我通常通过从记事本++启动Chrome中的index.html文件来运行我的测试。但是,因为我想使用Fiddler的JSON欺骗功能,所以我遇到了跨源策略限制。我有Fiddler的autoresponder工具匹配给定的URI(这里, ),然后返回一个预先写在文件中的JSON响应。



XMLHttpRequest无法加载。原始null不被Access-Control-Allow-Origin允许。

代码只是简单得多(jQuery 1.9.1):

  $(document).ready(function(){
$ .getJSON(http:// server。 (数据){
$ .each(data,function(i,item){
console.log('Item number:',i);
});
});
});

有没有更好的方法来做到这一点?也许在Chrome的开发工具?



谢谢! 您不需要另外的网络服务器,你可以使用Fiddler的AutoResponder来完成。只需编辑有问题的规则,以使访问控制允许源响应标题包含请求网站的来源的值。



如果您需要执行非简单(CORS术语)请求,请添加规则像这样:

方法:OPTIONS PartialTargetURL 在规则列表中使用 * CORSPreflightAllow



的动作, 之前的规则返回目标响应。此规则将导致Fiddler对XMLHttpRequest对象发送的预检请求作出肯定响应。


I am working on developing a modern webapp using JSON data. I found the following blog post on using a tool called Fiddler to mock JSON data.

I am developing locally using Notepad++, and primarily testing on Chrome (I will eventually focus on more browsers). I have a main HTML file "index.html" and I use a file structure that looks like this:

index.html

    assets

        /js

        /css

        /img

I usually run my tests by launching the index.html file in Chrome from Notepad++. However, because I want to use the JSON "spoofing" capabilities of Fiddler, I run into the cross-origin policy restriction. I have Fiddler's autoresponder tool match a given URI (here, http://server.anywhere.com/test), and then return a JSON response pre-fabricated in a file.

XMLHttpRequest cannot load http://server.anywhere.com/test. Origin null is not allowed by Access-Control-Allow-Origin.

The code is just about as simple as it gets (jQuery 1.9.1):

$(document).ready(function(){
    $.getJSON("http://server.anywhere.com/test", function(data) {
        $.each( data, function( i, item ) {
            console.log('Item number: ', i);
        });
    });
});

Is there a better way to do this? Perhaps a dev tool in Chrome?

Thanks!

解决方案

You don't need another webserver for this, you can do it all with Fiddler's AutoResponder. Simply edit the rule in question to have an Access-Control-Allow-Origin response header that contains the value of the origin of the requesting site.

If you need to perform a "non-simple" (CORS terminology) request, add a rule like so:

Method:OPTIONS PartialTargetURL with an action of *CORSPreflightAllow

to the list of rules, before the rule returning the target response. This rule will cause Fiddler to response affirmatively to the preflighting request sent by the XMLHttpRequest object.

这篇关于Cross Origin Policy& Fiddler JSON调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 00:57