【发布时间】:2015-01-30 10:32:06
【问题描述】:
我有一个以 xml 数据返回的 Web 服务。它只需要一个参数,用户标识。它工作正常,直到我想从该 Web 服务中检索数据并附加到我的 C# asp.net 中。当我运行脚本时,它总是落入错误(“未找到贷款”),其中实际上有相同参数输入的结果。
我已经
- 在我的脚本中硬编码。 (userid-'18450')
- 在脚本的 content-type 中将结果设置为 json
- 检查了我的网络服务的网址
但我不知道为什么它没有从 Web 服务获取数据。请帮忙。
我的网络服务代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
namespace WebAppTest2
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 WebService1 : System.Web.Services.WebService
{
public class Record
{
public int loanid { get; set; }
//public string daterequested { get; set; }
public string name { get; set; }
public string remark { get; set; }
//public string loandisplayid { get; set; }
public string status { get; set; }
public string nothing { get; set; }
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod()]
public List<Record> GetData(int userid)
{
SqlConnection con = new SqlConnection("Data Source=DIT-NB1260382;Initial Catalog=loansystem;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Equipment_ListPendingApproval ", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@userid", userid);
SqlDataReader dr = cmd.ExecuteReader();
List<Record> Record = new List<Record>();
while (dr.Read())
{
Record.Add(new Record()
{
loanid = dr.GetInt32(0),
//daterequested = dr.GetString(1),
name = dr.GetString(2),
//loandisplayid = dr.GetString(3),
status = dr.GetString(4),
remark = dr.GetString(5),
});
}
dr.Close();
con.Close();
return Record;
}
}
}
我的输出
我在 asp.net 中的脚本:
<script>
$(document).ready(function () {
//var userid = JSON.parse(sessionStorage.getItem('userID'));
var userid = JSON.parse('18450');
alert(userid);
$.ajax({
type: "POST",
url: "http://localhost:52928/WebService1.asmx/GetData",
data: JSON.stringify({ userid: userid }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var loans = response.d;
$.each(loans, function (index, Record) {
var loanid = this.loanid;
//construct your html here
var loanListDiv = "";
loanListDiv += "<li>";
loanListDiv += "<a href='requestCollectionPopup.html' data-rel='dialog' data-transition='pop' class='ui-btn ui-btn-icon-right ui-icon-carat-r'>";
loanListDiv += "<span class='ui-li-count ui-body-inherit' style='border:none; color:#800080; background-color:transparent;'>";
loanListDiv += Record.status;
loanListDiv += "</span>";
loanListDiv += Record.loanid;
loanListDiv += "</a>";
loanListDiv += "</li>";
$("#loansAll").append(loanListDiv).trigger("create");
});
},
error: function (response) {
alert("No loans Found");
}
});
});
</script>
【问题讨论】:
-
还添加标签 JavaScript、AJAX 和 jQuery
-
你能把你的整个代码贴在这里
标签: javascript jquery asp.net ajax web-services