2016-08-03 153 views
0

我向Godaddy发布了一个ASP.NET MVC应用程序。我遇到了一个Ajax调用问题,它应该返回一个JSON对象,它返回我网站索引页的HTML。我之前在应用程序的菜单栏链接中遇到了问题,他们将其重定向到我网站的主页面。我可以通过向我的网站的web.config添加一条规则来排除包含该应用程序的子文件夹,从而解决了该问题:<add input="{REQUEST_URI}" pattern="^/(codesnippetapp)" negate="true" />我在Chrome中检查了开发控制台,请求URL错误。网址应为http://www.mattdailey.net/codesnippetapp/Home/GetCodeData取而代之的则是http://www.mattdailey.net/Home/GetCodeDatajsonresult正在返回html而不是json

这里是Ajax调用和检索JSON的JsonResult功能:

$.ajax({ 
      url: '/Home/GetCodeData', 
      type: 'Post', 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json', 
      data: JSON.stringify(selectedSnippetID), 
      success: function (data) { 
       if (data.success) { 
        $("#snippetcode").val(data.snippetCode);      
       } else { 
        alert('invalid ID' + data.success); 
       } 
      } 
     }); 
    [HttpPost] 
    public JsonResult GetCodeData(int snippetID) 
    { 

     CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID); 
     if (returnedsnippet != null) 
     { 
      return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode }); 
     } 
     return Json(new { success = false }); 

    } 

什么我需要添加到我的应用程序的web.config?或者我需要将代码添加到我的网站的web.config?

更新: 我尝试使用GET方法,但得到了内部服务器错误。

@section Scripts 
{ 
... jQuery code 
} 

随后已将此添加到_Layout.cshtml:

@RenderSection("Scripts", required: false) 

我没加我的剃须刀用剃刀@section这样的代码移动从外部文件进入查看自己的脚本@ Url.Action助手到Ajax网址。我也改变了我向Godaddy发布应用程序的方式,我认为这也有帮助。我从FTP方法更改为Filesystem。然后我通过FTP手动上传文件。它正在工作。

谢谢大家的帮助。我写下了我的步骤,希望这可以帮助处于类似情况的其他人。

回答

0

/在url值的开头将使其成为您网站的根(而不是您的应用在该下)。

使用Url.Action辅助方法来生成操作方法的路径。

url: '@Url.Action("GetCodeData","Home")', 

这应该工作,如果你的JavaScript是在剃刀视图内。如果您的代码是一个外部JS文件内,呼叫在剃刀视图此方法,并把它分配给一个变量,并用它在js文件如在this post

0

使用第二部分解释JsonRequestBehavior.AllowGet

public JsonResult GetCodeData(int snippetID) 
{ 

    CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID); 
    if (returnedsnippet != null) 
    { 
     return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode },JsonRequestBehavior.AllowGet); 
    } 
    return Json(new { success = false },JsonRequestBehavior.AllowGet); 

} 
+0

您不需要为** HttpPost **操作/调用(OP正在执行)指定'JsonRequestBehaviour.AllowGet'。只有当你从GET操作方法返回一些json时才需要它。 – Shyju

0

这应该是一个GET操作,因为您试图将Json返回给客户端。

$.ajax({ 
      url: '/Home/GetCodeData', 
      type: 'GET', 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json', 
      data: JSON.stringify(selectedSnippetID), 
      success: function (data) { 
       if (data.success) { 
        $("#snippetcode").val(data.snippetCode);      
       } else { 
        alert('invalid ID' + data.success); 
       } 
      } 
     }); 

    public JsonResult GetCodeData(int snippetID) 
    { 

     CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID); 
     if (returnedsnippet != null) 
     { 
      return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode }, JsonRequestBehavior.AllowGet); 
     } 
     return Json(new { success = false }, JsonRequestBehavior.AllowGet); 

    }