2010-05-17 47 views
0

我使用jQuery AJAX調用Web服務方法,但不這樣做,併產生錯誤..Web服務方法不能從jQuery的AJAX訪問

的代碼是這裏在asp網頁jQuery的AJAX

var indexNo = 13; //pass the value 

    $(document).ready(function() { 
     $("#a1").click(function() { 
     $.ajax({ 
       type: "POST", 
       url: "myWebService.asmx/GetNewDownline", 
       data: "{'indexNo':user_id}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function(msg) { 
       $("#divResult").text(msg.d); 
       } 
     }); 
    }); 
    }); 

,這是爲Web服務方法

using System; 
using System.Collections; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using System.Xml.Linq; 
using System.Data; 
using System.Web.Script.Serialization; 
using TC.MLM.DAL; 
using TC.MLM.BLL.AS; 

/// <summary> 
/// Summary description for myWebService 
/// </summary> 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

[System.Web.Script.Services.ScriptService] 

public class myWebService : System.Web.Services.WebService 
{ 
    public myWebService() 
    { 
     //Uncomment the following line if using designed components 
     //InitializeComponent(); 
    } 

    [WebMethod] 
    public string HelloWorld() 
    { 
     return "Hello World"; 
    } 

    [WebMethod] 
    public string GetNewDownline(string indexNo) 
    { 
     IndexDetails indexDtls = new IndexDetails(); 
     indexDtls.IndexNo = "13"; 
     DataSet ds = new DataSet(); 
     ds = TC.MLM.BLL.AS.Index.getIndexDownLineByIndex(indexDtls); 
     indexNoDownline[] newDownline = new indexNoDownline[ds.Tables[0].Rows.Count]; 

     for (int count = 0; count <= ds.Tables[0].Rows.Count - 1; count++) 
     { 
      newDownline[count] = new indexNoDownline(); 
      newDownline[count].adjustedid = ds.Tables[0].Rows[count]["AdjustedID"].ToString(); 
      newDownline[count].name = ds.Tables[0].Rows[count]["name"].ToString(); 
      newDownline[count].structPostion = ds.Tables[0].Rows[count]["Struct_Position"].ToString(); 
      newDownline[count].indexNo = ds.Tables[0].Rows[count]["IndexNo"].ToString(); 
      newDownline[count].promoterId = ds.Tables[0].Rows[count]["PromotorID"].ToString(); 
      newDownline[count].formNo = ds.Tables[0].Rows[count]["FormNo"].ToString(); 
     } 

     JavaScriptSerializer serializer = new JavaScriptSerializer(); 
     JavaScriptSerializer js = new JavaScriptSerializer(); 
     string resultedDownLine = js.Serialize(newDownline); 

     return resultedDownLine; 
    } 

    public class indexNoDownline 
    { 
     public string adjustedid; 
     public string name; 
     public string indexNo; 
     public string structPostion; 
     public string promoterId; 
     public string formNo; 
    } 
} 

請幫我的東西。

+1

什麼錯誤? – 2010-05-17 07:35:15

回答

0

輸入的JSON數據存在問題。您應該嘗試使用內置的JSON類,而不是手動序列化。防爆。

$.ajax({ 
    ... 
    data: JSON.stringify({ indexNo: user_id }), 
    ... 
}); 

這應該解決您的問題。

0

你應該改變藝術,如何在服務器中實現序列化。只需添加ResponseFormat = ResponseFormat.Json的ScriptMethod屬性即可。 HTTP GET的使用,也可以對於額外的(可選)屬性的UseHttpGet = true

[WebMethod] 
[ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
public string GetNewDownline(string indexNo) 

數據庫訪問可以,如果你會使用SqlConnectionSqlCommandSqlDataReader類或DbDataReader另一版本中也得到了改善取決於你的數據庫源(OleDbDataReaderOracleDataReaderOdbcDataReader,DataTableReader)。如果您喜歡使用強類型數據,那麼由Visual Studio生成的SqlDataAdapter將是DataSet的更好版本。要做到這一點,只需添加一個新的項目到你的主題,選擇「數據」/「數據集」,然後添加一個查詢或TableAdapter

如果您決定使用HTTPGET您應該不對web.config文件進行相應修改。

<configuration> 
    <!-- ... --> 
    <system.web> 
    <webServices> 
     <protocols> 
     <add name="HttpGet"/> 
     </protocols> 
    </webServices> 
    <!-- ... --> 
    </system.web> 
</configuration> 

關於JSON.stringify使用我有一個像 「egyedg」 相同的看法。

我建議你看看下面的鏈接:

Can I return JSON from an .asmx Web Service if the ContentType is not JSON?

How do I build a JSON object to send to an AJAX WebService?

JQuery ajax call to httpget webmethod (c#) not working