在新窗口中打开jquery get请求

时间:2014-10-16 14:15:31

标签: jquery asp.net-mvc

我尝试进行ajax调用以获取下拉列表中的数据,调用控制器并在新窗口中获取结果。

到目前为止,我已经尝试过这个:

$(document).ready(function () {

    $('#seeTemplates').click(function () {
        var template = $('#templates').val();

        alert("templateOpen!");

        alert(template);

        window.open($.get("@Url.Action("SeeTemplateDetailsByName", "EbayTemplate")", {
            templateName: template
        }));
    });
}

因此,当用户点击链接时,我希望获得的数据将在新窗口中打开。

这是控制器帖子:

public ActionResult SeeTemplateDetailsByName(string templateName)
{
    EbayTemplateInfo ebayTemplateToShow = mEbayTemplateManager.GetTemplateByName(templateName);

    if (ebayTemplateToShow == null)
    {
        TempData[MessageDomain.Tags.TEMPDATA_MESSAGE_ERROR] = NODATAFOUND;

        return RedirectToAction("EbayTemplateSearchIndex");
    }

    return View(ebayTemplateToShow);
}

它正常工作,但打开的窗口显示:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Card/[object Object]

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34212

网址是这样的:http://localhost:63779/Card/[object%20Object]而且我不明白为什么会这样。

2 个答案:

答案 0 :(得分:2)

您正在将promise传递给window.open,这不符合预期,请参阅here需要传递的参数。

您是否尝试过这样做:

window.open("/EbayTemplate/SeeTemplateDetailsByName?templateName=" + template);

答案 1 :(得分:1)

  1. 这是一个来电,而非帖子。当你使用$ .get时,Razor正在寻找一个“[System.Web.Http.HttpGet]”作为控制器上的一个属性。这可以确保您正在进行正确的调用,并且应用程序知道它是“获取”。

  2. 确保您的JSON对象与控制器上的传入值匹配。您可以创建要传递回控制器的对象的模型,并声明:“modelOjbect templateName”或者您可以确保在params中使用JSON.stringify(templateName)将对象作为字符串传回。你调用window.open(“URL?templateName =”+ JSON.stringify(templateName))。

  3. 希望有所帮助!

相关问题