通过 AJAX 拉入方法后调试 javascript
我正在使用 MVC 和 jquery 从服务器中提取一个函数(作为我动态生成并附加到 html 正文的部分视图)并执行它。这工作正常,我可以在 fiddler 中查看它,但调试很糟糕。我使用以下方法引入该方法:
$("#makeGrid").click(function (e) {
$.get('/gridder/basicgrid', callbackFn);
function callbackFn(data) {
//Append markup to dom
$('body').append(data);
// call the js function from the partialview here
generateGrid();
}
});
我不确定这是否是最佳实践,但如果我在 ajax 命令后“查看源代码”,则代码不可见,并使用调试器;命令似乎不起作用。例如:
function generateGrid() {
alert("start");
debugger;
alert("end");
}
创建两个警报,但即使 Firebug 处于活动状态,也不会启动调试器。此讨论提出了类似的问题。有些人通过使用调试器两次(这个错误现在应该消失了)或在新窗口中打开 firebug 来解决这个问题(不走运)。甚至 eval('调试器;');另一个线程中有人建议但不好!
有什么建议吗? (包括如果需要的话使用 firebug 以外的工具,但我想调试,而不是查看 fiddler 风格)
I am using MVC and jquery to pull in a function from the server (as a partial view which I generate on the fly and append to the body of the html) and execute it. This works fine, I can view it in fiddler, but debugging is terrible. I pull in the method using something like:
$("#makeGrid").click(function (e) {
$.get('/gridder/basicgrid', callbackFn);
function callbackFn(data) {
//Append markup to dom
$('body').append(data);
// call the js function from the partialview here
generateGrid();
}
});
Whether this is best practice or not I'm not sure, but if I 'view source' after the ajax command, the code isn't visible, and using the debugger; command doesn't seem to work. Eg:
function generateGrid() {
alert("start");
debugger;
alert("end");
}
Creates the two alerts but doesn't bring up the debugger even though firebug is active. This discussion raises a similar issue. Some worked around it by using debugger twice (this bug meant to be gone now) or opening firebug in a new window (no luck). Even eval('debugger;'); was suggested by someone in another thread but no good!
Any suggestions? (including using a tool other than firebug if needed, but I want to debug, not view fiddler-style)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来这个问题还没有得出结论性的答案。为了供其他人参考,我最终在VS2010本身中进行了调试,如下:
a)必须使用IE作为VS默认浏览器。 Firefox 不喜欢玩。如果您使用 MVC,默认浏览器选择器不会像在 Web 表单中那样遇到视图等。一个快速的解决方案是在根目录中创建一个空的 .html 页面,然后右键单击该混蛋以获取“浏览方式...”,然后选择所需的默认浏览器。多田。
b) 接下来,在 IE 的高级选项中,确保取消选中“禁用 javascript 调试”选项。
c) 现在,您可以使用调试器了;在视图中的脚本中添加命令来中断动态生成的脚本的执行。它将返回到 VS 并突出显示该行。因为它是生成的,所以顶部显示[动态脚本]。断点似乎仍然不起作用。
d) 在IE中,js错误也会在控制台中报告。它会给你一个行号和列,突出显示你的文件。如果您发送了动态局部视图,它会在原始页面文件中突出显示这些坐标,这是不适用的。相反,请转到 VS 的“脚本块 [动态]”选项卡中的等效坐标。
事实上,这对于 MVC 开发来说可能比 Firebug 提供的更有用。
干杯
Doesn't look like a conclusive answer is coming on this one. For others' reference, I ended up getting debugging going in VS2010 itself, as follows:
a) Must use IE as VS default browser. Firefox doesn't like playing. If you're using MVC, the default browser selector doesn't come up against views etc like it does in webforms. A quick solution is to create an empty .html page in the root and right click that bastard to get 'browse with...' and select the desired default browser. Tada.
b) Next, in IE's advanced options, make sure you untick the 'disable javascript debugging' options.
c) Now, you can use the debugger; command in your script within the view to break execution of your dynamically generated script. It will go back to VS and highlight the line. Because it's generated it says [dynamic script] at the top. Breakpoints still don't seem to work annoyingly.
d) In IE, the js errors also are reported in the console. It will give you a line number and column, highlighting your file. If you have sent a dynamic partial view though, it highlights these coordinates in your original page file, which is not applicable. Rather, go to the equivalent coordinates in the "script block [dynamic]" tab of VS.
In truth this is probably more useful for MVC development than what Firebug would have offered.
Cheers