2016-08-03 148 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); 

    }