通过javascript和html5调用C#WCF json服务
我很难从javascript调用WCF服务(json)。这是我为从数据库获取数据而创建的一个简单服务。用户应该能够从HTML5网站提供用户名和密码,点击提交按钮,服务应该被调用 这是服务接口通过javascript和html5调用C#WCF json服务,javascript,jquery,ajax,html,wcf,Javascript,Jquery,Ajax,Html,Wcf,我很难从javascript调用WCF服务(json)。这是我为从数据库获取数据而创建的一个简单服务。用户应该能够从HTML5网站提供用户名和密码,点击提交按钮,服务应该被调用 这是服务接口 [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Js
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/getLogin", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int getLogin(string username, string password);
这就是服务方法
public int getLogin(string username, string password)
{
var conString = ConfigurationManager.AppSettings.Get("MyDBConnectionStrings");
var sqlConn = new SqlConnection(conString);
var restID = 0;
var user_name = "";
var pass_word = "";
try
{
sqlConn.Open();
var query = "select * from Restaurant where businesscode = '" + username + "' and password = '" + password + "'";
var createCommand = new SqlCommand(query, sqlConn);
createCommand.ExecuteNonQuery();
var dr = createCommand.ExecuteReader();
if (dr.Read())
{
user_name = dr["BusinessCode"].ToString();
pass_word = dr["password"].ToString();
restID = Convert.ToInt32(dr["RestuarantID"].ToString());
}
else
{
return restID;
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
sqlConn.Close();
}
return restID;
}
这是我用来在点击按钮时调用服务的javascript函数
function loginService()
{
var businesscode = document.getElementById("buscode").value;
var password = document.getElementById("pass").value;
var serviceUrl = "http://localhost:18933/Service1.svc/getLogin";
var dataString='username=' + businesscode + '&password=' + password;
$.ajax({
type: "POST",
url: serviceUrl,
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
console.log(result);
if(result == 1)
{
alert("log successful");
}
else
{
alert("fail");
}
}
});}
我不知道我哪里出错了。当我点击我调用函数的submit按钮时,网站没有响应,控制台也没有显示错误。如果有人能以某种方式帮助我,那将是一个很大的帮助。谢谢..您是否输入了请求正文?您的数据(dataString变量)不是JSON格式。