2014-03-28 23 views
0

我不知道這段代碼有什麼問題。我無法正確實施。 在此先感謝您的幫助。 這是我到目前爲止已經試過並堅持將參數傳遞給使用jquery ajax的方法後面的代碼出現錯誤

<script type="text/javascript"> 
      $(document).ready(function() { 
       for (var key in localStorage) { 
        GetQuickVL(key); 
       } 
      }); 
      function GetQuickVL(key) { 
       if (key.substring(0, 4) == "vhs-") { 
        $.ajax({ 
         type: "POST", 
         url: "/QuickViewList.aspx/GetQuickVD", 
         data: '{key: ' +'1' + '}', 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 
         success: OnSuccess, 
         failure: function (response) { 
          alert(response.response); 
         }, 
         error: function (response) { 
          alert(response.error); 
         } 
        }); 
       } 
      } 
      function OnSuccess(response) { 
       alert('df'); 
      } 
</script> 

和C-夏普代碼

[WebMethod] 
public int GetQuickVD(int key) 
{ 
    return key; 
} 
+0

控制檯中是否有錯誤? –

+0

我得到一個奇怪的錯誤,但我不能發佈的圖像,因爲我沒有10個信譽點 – user3472352

+1

@ user3472352,無需發佈圖像 - 文本的錯誤就足夠了 – Andrei

回答

0

如果該方法是在後面的代碼(.aspx.cs文件)嘗試聲明GetQuickVD方法靜態的。所以它是公共靜態int GetQuickVD(int鍵)。

+0

我想知道是否有人可以告訴我爲什麼我在做數據時出錯:'{key:'+'1,1,1,1 , '+'}'; – user3472352

1

keyint。你已經通過它作爲一個string

'{key: ' +'1' + '}', 

要麼是:

{key: 1 }, 

或者讓你的Web方法採取objectstring作爲它的參數:

[WebMethod] 
public int GetQuickVD(object key) 
{ 
    return Convert.ToInt32(key); 
} 

這裏是一個完整的工作樣本(我剛測試過)。適應滿足您的需求:

的WebService:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[System.Web.Script.Services.ScriptService] // <- ** MAKE SURE THIS IS UNCOMMENTED! 
public class WebService1 : System.Web.Services.WebService 
{ 
    [WebMethod] 
    public int GetQuickVD(int key) 
    { 
     return key; 
    } 
} 

ASPX頁面:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('.foo').click(function() { 
      $.ajax({ 
       type: "POST", 
       url: "WebService1.asmx/GetQuickVD", 
       data: '{key: ' + '1' + '}', 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function() { 
        alert("success"); 
       } 
      }); 
     }) 
    }); 
</script> 
<a href="#" class="foo">click me</a> 
+0

嘗試過您的解決方案,但無法正常工作 – user3472352

+0

定義'not working'...?你收到一條錯誤消息嗎? – DGibbs

+0

獲得我之前發佈的同樣的錯誤 – user3472352

相關問題