2015-06-12 15 views
1

我對asp.net MVC相當陌生,但對於爲什麼我的請求不起作用感到困惑。ajax請求不發送任何數據使用jQuery的ASP.NET MVC項目

我試圖用jQuery發送Ajax請求按:

jQuery(function ($) { 
    var total = 0, 
     share = $('div.share'), 
     googlePlusUrl = "https://plusone.google.com/_/+1/fastbutton?url=http://bookboon.com" + $(location).attr('pathname'); 

    setTimeout(function() { 
     $.ajax({ 
      type: 'GET', 
      data: "smelly", 
      traditional: true, 
      url: share.data('proxy'), 
      success: function(junk) { 
       //var $junk = junk.match(regex); 
       console.log(junk); 
      }, 
      error: function (xhr, errorText) { 
       console.log('Error ' + xhr.responseType); 
      }, 
     }); 
    }, 4000); 

在我RouteConfig設置路線爲:

routes.MapRoute(null, "services/{site}/proxy", new { controller = "Recommendations", action = "Proxy" }); 

的標記具有數據屬性值:

<div class="share" data-proxy="@Url.Action("Proxy", "Recommendations")"> 

而我代理的操作方法與開始

public ActionResult Proxy(string junk) 

問題是junk參數始終爲空。我可以在調試輸出中看到,當頁面加載時(按照jQuery的文檔準備功能),路由似乎正確地重定向到此方法,但我似乎無法發送任何數據。

我試過發送簡單的數據(「臭」),但我也沒有收到。

任何建議表示讚賞!

回答

2

模型聯編程序將在請求中尋找名爲junk的參數,但是您只發送一個純字符串。試試這個吧:

$.ajax({ 
    type: 'GET', 
    data: { junk: "smelly" }, // <- note the object here 
    traditional: true, 
    url: share.data('proxy'), 
    success: function(junk) { 
     //var $junk = junk.match(regex); 
     console.log(junk); 
    }, 
    error: function (xhr, errorText) { 
     console.log('Error ' + xhr.responseType); 
    }, 
}); 
+1

你的反應很快樂,你很難糾正間距..讓我所有的寶寶都好好的先生 – JensenB