2013-05-03 64 views
0

我想知道是否有人可以指出我正确的方向.....我刚刚开始使用jSON和c#,并且遵循一个教程(用一些黑客)来返回一些数据回到网页。我现在拥有的是这个(缩写)。使用json和数据

具有Web服务调用存储过程的asp.net的解决方案:该解决方案

[WebMethod] 
    public string ReadRegion() 
    { 
     DataSet myDS = getInterests(); 
     StringBuilder sb = new StringBuilder(); 
     DataTable myVenues = myDS.Tables[0]; 
     if (myVenues.Rows.Count > 0) 
     { 
      foreach (DataRow myVenueRow in myVenues.Rows) 
      { 
       sb.Append(myVenueRow["descript"].ToString().TrimEnd() + "<br/>"); 
      } 
     } 
     else 
     { 
      sb.Append("No Records Found"); 
     } 
     return sb.ToString(); 
    } 

我也有一个aspx页面有以下几点:

<script type = "text/javascript"> 
    function ShowRegionsInfo() { 
     var pageUrl = '<%=ResolveUrl("~/WebService/wsJQueryDBCall.asmx")%>' 

     $.ajax({ 
      type: "POST", 
      url: pageUrl + "/ReadRegion", 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: OnSuccessCall, 
      error: OnErrorCall 
     }); 
    } 

    function OnSuccessCall(response) { 
     $('#<%=lblOutput.ClientID%>').html(response.d); 
    } 

    function OnErrorCall(response) { 
     alert(response.status + " " + response.statusText); 
    } 
</script> 

的代码工作正如我得到页面上的描述(离子)列表。但是,我希望在返回的记录集中包含一系列行,并最终希望能够从客户端动态过滤结果。

所以我的问题是 我开始了错误的脚即ie。我是否以正确的格式将数据返回到页面?因为所有其他jSon示例,说实话,看起来都不同于我的!如果我在错误的方面,有谁能给我一些建议,我应该采取什么措施。

感谢您提供的任何建议!

克雷格

回答

0

设置dataType: "json"在你的Ajax选项告诉你期待来自服务器的JSON响应jQuery的。你只是从服务器(这是默认的)返回HTML,所以删除该行。

此外,在您的OnSuccessCall()方法中,您可以将response.d更改为response

<script type = "text/javascript"> 
    function ShowRegionsInfo() { 
     var pageUrl = '<%=ResolveUrl("~/WebService/wsJQueryDBCall.asmx")%>' 

     $.ajax({ 
      type: "POST", 
      url: pageUrl + "/ReadRegion", 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      success: OnSuccessCall, 
      error: OnErrorCall 
     }); 
    } 

    function OnSuccessCall(response) { 
     $('#<%=lblOutput.ClientID%>').html(response); 
    } 

    function OnErrorCall(response) { 
     alert(response.status + " " + response.statusText); 
    } 
</script> 
+0

感谢您的。 我已经删除该行,但删除response.d现在不返回任何结果。 – SxChoc 2013-05-03 14:56:20

+0

数据是作为一组jSon数据实际返回还是只返回一个字符串? – SxChoc 2013-05-03 15:22:02

0

如果从aspx.cs调用特定的方法文件

ASPX页面使用下面的脚本

$('#<%= ddlItemCatogory.ClientID%>').change(function() { 
      var catId = this.value; 
      $('#<%= ddlItem.ClientID%>').get(0).options[0] = new Option("loading ... ", "0"); 
      $("#phMainContent_ctl00_ddlItem").resetSS(); 
      $.ajax({ 
       type: "POST", 
       url: "handler/PopulateAjaxData.aspx/GetItem", 
       data: "{'catId':'" + catId.toString() + "'}", 
       contentType: "application/json; charset=utf-8", 
       dataType: 'json', 
       success: function (data) { 
        if (data.d.length > 0) { 
         $('#<%= ddlItem.ClientID%>').get(0).options.length = 0; 
         $('#<%= ddlItem.ClientID%>').get(0).options[0] = new Option("Select Item", "0"); 
         $.each(data.d, function (index, item) { 
          $('#<%= ddlItem.ClientID%>').get(0).options[$('#<%= ddlItem.ClientID%>').get(0).options.length] = new Option(item.Display, item.Value); 
         }); 
        } 
        else { 
         $('#<%= ddlItem.ClientID%>').get(0).options.length = 0; 
         $('#<%= ddlItem.ClientID%>').get(0).options[0] = new Option("No Item found", "0"); 
        } 
        $("#phMainContent_ctl00_ddlItem").resetSS(); 
       }, 
       error: function() { 
        alert("Failed to load Item"); 
       } 
      }); 
     }); 

的代码PopulateAjaxData.aspx.cs

[WebMethod] 
    public static ArrayList GetItem(string catId) 
    { 
     ArrayList list = new ArrayList(); 
     PopulateAjaxData item = new PopulateAjaxData(); 
     List<DataModels.Items> items = item.loadItem(int.Parse(catId)); 

     foreach (DataModels.Items i in items) 
     { 
      list.Add(new { Value = i.Id.ToString(), Display = i.Name }); 
     } 

     return list; 
    } 

如果调用aspx.cs文件,然后使用下面的代码...

在.aspx页面中使用下面的代码

function addToCart() { 
     var vehicleId = $('#<%= VehicleId.ClientID%>').val(); 
     var parameter = "vehicleId=" + vehicleId; 

     $.ajax({ 
      type: "POST", 
      url: "handler/AddToCart.aspx?" + parameter, 
      success: function (msg) { 

       if (msg == "1") { 
        $('#message').html('Vehicle has been added to Cart.'); 
       } 
       else { 
        $('#message').html('Vehicle can not be added at this moment.'); 
       } 
      } 
     }); 
    } 

aspx。CS加载事件使用以下代码

protected void Page_Load(object sender, EventArgs e) 
{ 
    string vehicleId = Request.Params["vehicleId"] ?? ""; 
    string productId = Request.Params["productId"] ?? ""; 
    string message = string.Empty; 
    int CartId = 0; 


    if (Request.Cookies["CartId"] == null) 
    { 
     if (CartId == 0) 
     { 
      DataModels.Cart cart = new DataModels.Cart(); 
      cart.CreatedAt = DateTime.Now; 

      try 
      { 
       cart = Service.Create(cart); 
       if (cart.Id > 0) 
       { 
        HttpCookie objCookie = new HttpCookie("CartId"); 
        objCookie.Value = cart.Id.ToString(); 
        objCookie.Expires = DateTime.Now.AddDays(1); 
        Response.Cookies.Add(objCookie); 
        CartId = cart.Id; 
       } 
      } 
      catch (Exception ex) 
      { 
       message = ex.Message; 
       Response.Write("-1"); 
      } 
     } 
    } 
    else 
    { 
     CartId = int.Parse(Request.Cookies["CartId"].Value); 
    } 

    DataModels.CartItem cv_new = new DataModels.CartItem(); 
    if (CartId > 0 && !(string.IsNullOrEmpty(vehicleId))) 
    { 
     DataModels.CartItem cv = Service.ReadCartByVehicleId(CartId, int.Parse(vehicleId)); 

     if (cv == null) 
     { 
      cv_new.Cart = Service.ReadCart(CartId); 
      cv_new.Vehicle = Service.ReadVehicle(int.Parse(vehicleId)); 
      cv_new.Product = null; 
      cv_new = Service.Create(cv_new); 
     } 
    } 
    else if (CartId > 0 && !(string.IsNullOrEmpty(productId))) 
    { 
     DataModels.CartItem cv = Service.ReadCartByProductId(CartId, int.Parse(productId)); 

     if (cv == null) 
     { 
      cv_new.Cart = Service.ReadCart(CartId); 
      cv_new.Vehicle = null; 
      cv_new.Product = Service.ReadProduct(int.Parse(productId)); 
      cv_new = Service.Create(cv_new); 
     } 
    } 


    Response.Write("1"); 
} 
+0

嗨,谢谢你,但我仍然没有得到它! – SxChoc 2013-05-09 20:30:45