Javascript 如何使用Ajax成功在新选项卡中打开URL?
在这里,我试图通过使用Ajax Success调用医生控制器的ViewFile操作在new选项卡中打开文件,该操作位于函数Javascript 如何使用Ajax成功在新选项卡中打开URL?,javascript,c#,html,ajax,Javascript,C#,Html,Ajax,在这里,我试图通过使用Ajax Success调用医生控制器的ViewFile操作在new选项卡中打开文件,该操作位于函数abc(this)中,单击锚定标记 现在的问题是,所有内容都符合要求,但url无法在新选项卡中打开。 下面是我的Ajax <script> function abc(thisEvent) { debugger; var getDoCredId = $(thisEvent).attr('docCredId'); var parameter =
abc(this)
中,单击锚定标记现在的问题是,所有内容都符合要求,但url无法在新选项卡中打开。
下面是我的Ajax
<script>
function abc(thisEvent) {
debugger;
var getDoCredId = $(thisEvent).attr('docCredId');
var parameter = { id: getDoCredId };
$.ajax({
url: "/Doctor/ViewFile1",
type: "get",
dataType: "html",
data: parameter,
success: function (data) {
debugger;
if (data = true) {
debugger;
var getdoctorId = $(thisEvent).attr('docCredId');
var url = "/Doctor/ViewFile/" + getdoctorId;
window.open(url, "_blank");
}
else {
debugger;
showNotification("Error", "warning");
}
}
});
}
尝试将
async:false
添加到Ajax请求中
function abc(thisEvent) {
debugger;
var getDoCredId = $(thisEvent).attr('docCredId');
var parameter = { id: getDoCredId };
$.ajax({
async: false, // <<<----------- add this
url: "/Doctor/ViewFile1",
type: "get",
dataType: "html",
data: parameter,
success: function (data) {
debugger;
if (data = true) {
debugger;
var getdoctorId = $(thisEvent).attr('docCredId');
var url = "/Doctor/ViewFile/" + getdoctorId;
window.open(url, "_blank");
}
else {
debugger;
showNotification("Error", "warning");
}
}
});
}
功能abc(此事件){
调试器;
var getDoCredId=$(thisEvent.attr('docCredId');
var参数={id:getDoCredId};
$.ajax({
async:false,//因为这对于常规注释来说太长了,所以我将此作为一个答案发布,虽然它不能直接解决问题,因为我无法重现它,但可能会提供一些见解,让您检查与此简化示例相比代码中发生的差异
从jQuery ajax成功回调调用window.open()
,效果很好:
我使用了与您相同的模式,没有使用服务器代码,而是使用jsonplaceholder.typicode.com示例API
<> P>代码示例中有一些问题,您可能需要考虑,即使您没有征求有关它的评论,并且它与您的问题(可能)不直接相关:
if(data=true)
意味着数据总是真实的。如果你知道它是一个布尔值,你可能想做一个if(data==true)
,或者if(data)
如果你想接受任何真实值(true,{},“something”,42等等).根据Java代码以及您在jQuery ajax调用中如何定义响应格式判断,您似乎希望“data”变量结果是HTML而不是布尔值。您可能希望尝试删除数据类型:“HTML”
行,让jQuery根据从服务器返回的内容设置数据格式,和/或发送JSON格式的响应,如成功响应的POJO中的{result:true}
。然后确保data.result==true
,以确保您得到了预期的结果
您可能应该向标记DOM元素添加任意数据,如果您使用的是jQuery,请使用选择器访问它们。白色仅添加带有字符串值的随机属性可能会起作用,这被认为是对HTML和DOM的滥用,data-*
属性专门用于添加任何数据
在abc()
函数中,您在开始时获取属性的值(var getDoCredId=$(thisEvent).attr('docCredId');
),但在回调中,您试图再次获取该值。您确实不需要它,因为success()回调是abc()范围内的闭包函数,并且它已经可以访问该值,因此实际上不需要在回调中执行var getdoctorId=$(thisEvent).attr('docCredId');
我还建议将getDoCredId
变量命名为docCredId
。具有“get”前缀通常意味着它是一个getter函数或对某个getter的引用。同样,主函数的“thisEvent
”参数可能应该被称为“callerement
”或者类似的东西,因为它不是事件,所以它是在调用abc(this)时直接从DOM传递的实际元素
锚定的onClick事件处理程序中的
。这只是为了让阅读该代码的任何人都能更清楚地理解该代码,并且当您在未来几个月回到该代码并试图弄清楚发生了什么时,您自己也能更清楚地理解:)
它在哪里打开?@nomed:它甚至在任何地方都不会打开,问题是当发送AJAX请求时调用的Doctor
控制器的ViewFile1
操作?@Alexander:是,根据指定的条件,它会返回true或false,但问题是即使从ViewFile1返回true,操作url也不会返回打开浏览器控制台中是否有任何错误消息?请尝试将console.log(data)
添加到成功处理程序中。因为看起来您不会得到一个可接受的答案,您至少应该获得一个对有用的建议的投票。
public bool ViewFile1(int id)
{
var document = _doctorService.GetDoctorCredentialDetails(id);
string AttachPath = ConfigPath.DoctorCredentialsAttachmentPath;
string strFileFullPath = Path.Combine(AttachPath, document.AttachedFile);
string contentType = MimeTypes.GetMimeType(strFileFullPath);
bool checkFileInFolder = System.IO.File.Exists(strFileFullPath);
if (checkFileInFolder == true)
{
return true;
}
else
{
return false;
}
}
public ActionResult ViewFile(int id)
{
var document = _doctorService.GetDoctorCredentialDetails(id);
string AttachPath = ConfigPath.DoctorCredentialsAttachmentPath;
string strFileFullPath = Path.Combine(AttachPath, document.AttachedFile);
string contentType = MimeTypes.GetMimeType(strFileFullPath);
bool checkFileInFolder = System.IO.File.Exists(strFileFullPath);
bool filedata = System.IO.File.ReadAllBytes(strFileFullPath).Any();
byte[] filedata1 = System.IO.File.ReadAllBytes(strFileFullPath);
var cd = new System.Net.Mime.ContentDisposition
{
FileName = document.FileName,
Inline = true
};
Request.HttpContext.Response.Headers.Add("Content-Disposition", cd.ToString());
return File(filedata1, contentType);
}
function abc(thisEvent) {
debugger;
var getDoCredId = $(thisEvent).attr('docCredId');
var parameter = { id: getDoCredId };
$.ajax({
async: false, // <<<----------- add this
url: "/Doctor/ViewFile1",
type: "get",
dataType: "html",
data: parameter,
success: function (data) {
debugger;
if (data = true) {
debugger;
var getdoctorId = $(thisEvent).attr('docCredId');
var url = "/Doctor/ViewFile/" + getdoctorId;
window.open(url, "_blank");
}
else {
debugger;
showNotification("Error", "warning");
}
}
});
}