我有一个以xml数据返回的Web服务。它只需要一个参数userid。它工作正常,直到我想从该Web服务检索数据并附加到我的C#asp.net中。当我运行脚本时,总是会出现错误 - (“找不到贷款”),其中实际上有相同参数输入的结果。
我已经
了但我不知道为什么不从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>
答案 0 :(得分:1)
data
方法的$.ajax()
参数无效。它的名称为userid
,而不是getUserID
,对。 F12
)中的“网络”标签,查看服务器实际响应此AJAX请求的内容。type
&#34; POST&#34;当你能够通过GET请求获得回复时看起来很可疑答案 1 :(得分:1)
从您需要将该服务标记为脚本服务的任何服务调用Web服务。将以下属性添加到Web服务并尝试。
[System.Web.Script.Services.ScriptService]
请参阅以下链接
http://www.dotnetjalps.com/2010/08/calling-aspnet-web-service-from-jquery.html http://www.codeproject.com/Articles/529847/Calling-ASP-NET-WebService-from-jQuery