在调试使用AJAX的jQuery应用程序时,我经常需要查看服务返回给浏览器的json.所以我将把JSON数据的URL丢弃到地址栏中.
这对ASPNET很好,因为在编码错误的情况下,我可以在浏览器中看到ASPNET诊断:

但是当服务器端代码正常工作并实际返回JSON时,IE会提示我下载它,所以我看不到响应.

我可以让IE不这样做,换句话说,只是将它显示为纯文本?
我知道如果我将Content-Type标头设置为,我可以这样做text/plain.
但这特别是ASPNET MVC应用程序的上下文,当我在我的一个动作方法上使用JsonResult时,它自动设置响应.另外,我有点想保留适当的内容类型,而不是仅仅为了支持调试工作而改变它.
用IE9开始测试我的jQuery应用程序.看起来我可能在这里遇到麻烦.我注意到当我将JSON数据返回到Javascript方法时,我总是得到这个提示符:"你想打开还是保存这个文件?" 并为我提供了3个按钮:打开,保存和取消.当然,我的javascript基于JSON对象中设置的值进行操作,但由于IE9没有将其传递给脚本,我无法从那里执行后续操作.
其他人面临这个问题?这是一个快照.
我正在尝试上传文件并返回有关文件属性(名称,大小等)的json响应.它在除IE之外的所有浏览器中都能正常工
IE尝试将JSON下载为文件!
我有IE10并通过从调试器更改浏览器模式和文档模式在IE7到10上测试它.
我使用的是asp.net mvc4,文件上传操作有HttpPost属性,我使用return Json(myObject)返回json响应;
这是我的http标题
请求
Key Value
Request POST /File/UploadFile/ HTTP/1.1
Accept text/html, application/xhtml+xml, */*
Referer http://localhost:63903/
Accept-Language en-NZ
User-Agent Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Content-Type multipart/form-data; boundary=---------------------------7dc1e71330526
Accept-Encoding gzip, deflate
Host localhost:63903
Content-Length 1377002
DNT 1
Connection Keep-Alive
Cache-Control no-cache
Run Code Online (Sandbox Code Playgroud)
响应
Key Value
Response HTTP/1.1 200 OK
Server ASP.NET Development Server/11.0.0.0
Date Tue, 18 Dec 2012 23:44:19 GMT
X-AspNet-Version 4.0.30319
X-AspNetMvc-Version 4.0
Cache-Control private
Content-Type application/json; charset=utf-8
Content-Length 154 …Run Code Online (Sandbox Code Playgroud) 我有一个问题发生在IE 8和Firefox 6.0但不是Chrome 17.0.9.当我发布下面的frmMain时,我将它发送到一个测试页面,它只返回一个简单的JSON字符串ContentType: application/json; charset=utf-8.问题是IE和FF将提示我保存从服务器返回的JSON,而不是在我的jquery代码中点击success方法.但奇怪的是,如果我省略了<input name='File_1' type='file' />发布的表单,那么IE和FF 不会提示我保存我的JSON并且我的jquery成功代码会触发.
因此,发布的内容似乎与浏览器对返回的有效负载的反应有关(在IE和FF中).通过Fiddler,我已经验证了在每种情况下返回的有效负载完全相同.
有任何想法吗?
解决方案:请参阅下面的答案.从我可以收集的内容"text/html"是在执行jquery/ajax/json时返回的最佳跨浏览器内容类型.
码
<script>
$(function () {
$('#btnSave').click(function () {
$('#frmMain').ajaxSubmit({
success: function (data, statusText, xhr, $form) {
alert('test success');
},
fail: function (data, statusText, xhr, $form) {
alert('test fail');
}
});
});
});
</script>
<body>
<form id='frmMain' action='/test' method='post'>
<!--Omit the file input below to make it work-->
file: <input name='File_1' type='file' /><br />
name: <input name='json' value='{"id":5}' /><br />
<input type='button' …Run Code Online (Sandbox Code Playgroud) 好的,所以我从Internet Explorer获得这个奇怪的意外响应,同时用smartyin 测试文件上传php.
这里我的文件上传(view)简化为主要问题的智能代码,对于那些没有使用activecollab的人Router::assemble来说,只是形成一个带有从MVC读取的参数的URL.
界面http://iforce.co.nz/i/23bgy0fj.1cc.png
<div id="xero_invoice_manager_api">
{form action=Router::assemble('xero_invoice_manager_api') method=post id="xero_invoice_manager" enctype="multipart/form-data"}
<div class="content_stack_wrapper">
<input type="file" name="file_1" /><br/>
<input type="file" name="file_2" /><br/>
{wrap_buttons}
{submit success_event="api_updated" }Authenticate{/submit}
{/wrap_buttons}
{/form}
</div></div>
Run Code Online (Sandbox Code Playgroud)
这是我的jquery view.
App.Wireframe.Events.bind('api_event_finished.content', function(event, settings) {
App.Wireframe.Flash.success(App.lang('Xero Invoice Manager has saved/uploaded your Xero API data.'));
});
Run Code Online (Sandbox Code Playgroud)
这是我的简化控制器(我发现问题是smarty而不是php).
//api view
function api(){
if ( $this->request->isSubmitted()) {
$this->response->respondWithData(true);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器上传发生..
//api view
function api(){
$this->assignSmarty();
if ($this->request->isSubmitted()) {
$this->XeroAuthUpdate(); //update everything
if(isset($_FILES)){
$file_manager …Run Code Online (Sandbox Code Playgroud) 我没有用过Form元素.我没有使用表格,因为,我不想回发..请指导我如何做ajax调用所以,能够得到$ .ajax将viewmodel发布到控制器的动作方法?我的表格如下:
HTML:
@model comp.learn.data.Models.ProductViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<fieldset>
<legend>ProductViewModel</legend>
<div id="CreateDiv">
<div class="editor-label">
@Html.LabelFor(model => model.ProductName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductName)
@Html.ValidationMessageFor(model => model.ProductName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Cost)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Cost)
@Html.ValidationMessageFor(model => model.Cost)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductTypeId)
</div>
<div class="editor-field">
@Html.DropDownList("ProductTypeId", "Choose item")
@Html.ValidationMessageFor(model => model.ProductTypeId)
</div>
<div class="editor-label">
@Html.LabelFor(model …Run Code Online (Sandbox Code Playgroud) jquery ×4
json ×3
ajax ×2
asp.net-mvc ×2
activecollab ×1
asp.net-ajax ×1
c# ×1
c#-4.0 ×1
firefox ×1
http-headers ×1
php ×1
smarty ×1