调用WCF usng Jquery ajax
我已经浏览了很多帖子,但没有得到任何帮助。我试图使用jQueryAjax调用我的WCf,但我总是会遇到以下错误调用WCF usng Jquery ajax,jquery,wcf,Jquery,Wcf,我已经浏览了很多帖子,但没有得到任何帮助。我试图使用jQueryAjax调用我的WCf,但我总是会遇到以下错误 400(错误请求) 访问控制允许源 我的代码如下: Web.config[服务] <?xml version="1.0"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="UserDetails1Behavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<!--<bindings>
<webHttpBinding>
<binding name="UserDetailsBinding" crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
</bindings>-->
<bindings>
<wsHttpBinding>
<binding name="wsbind">
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="UserDetails1Behavior" name="UserDetails1.Service1">
<endpoint address=""
binding="wsHttpBinding" contract="UserDetails1.IService1"
/>
<!--<endpoint address="mex" binding="wsHttpBinding" contract="IMetadataExchange"/>-->
</service>
</services>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace UserDetails1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public DataSet SelectUserDetails()
{
SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=registration;User ID=sa; Password=saviant");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from RegistrationTable", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
cmd.ExecuteNonQuery();
con.Close();
return ds;
}
public string InsertUserDetail(UserDetails userInfo)
{
string Message;
SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=registration;User ID=sa; Password=saviant");
con.Open();
SqlCommand cmd = new SqlCommand("insert into RegistrationTable(UserName,Password,Country,Email) values(@UserName,@Password,@Country,@Email)", con);
cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
cmd.Parameters.AddWithValue("@Password", userInfo.Password);
cmd.Parameters.AddWithValue("@Country", userInfo.Country);
cmd.Parameters.AddWithValue("@Email", userInfo.Email);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
Message = userInfo.UserName + " Details inserted successfully";
}
else
{
Message = userInfo.UserName + " Details not inserted successfully";
}
con.Close();
return Message;
}
public void UpdateRegistrationTable(UserDetails userInfo)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=saviant");
con.Open();
SqlCommand cmd = new SqlCommand("update RegistrationTable set UserName=@UserName,Password=@Password,Country=@Country, Email=@Email where UserID=@UserID", con);
cmd.Parameters.AddWithValue("@UserID", userInfo.UserID);
cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
cmd.Parameters.AddWithValue("@Password", userInfo.Password);
cmd.Parameters.AddWithValue("@Country", userInfo.Country);
cmd.Parameters.AddWithValue("@Email", userInfo.Email);
cmd.ExecuteNonQuery();
con.Close();
}
public bool DeleteUserDetails(UserDetails userInfo)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=saviant");
con.Open();
SqlCommand cmd = new SqlCommand("delete from RegistrationTable where UserID=@UserID", con);
cmd.Parameters.AddWithValue("@UserID", userInfo.UserID);
cmd.ExecuteNonQuery();
con.Close();
return true;
}
public DataSet UpdateUserDetails(UserDetails userInfo)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=saviant");
con.Open();
SqlCommand cmd = new SqlCommand("select * from RegistrationTable where UserID=@UserID", con);
cmd.Parameters.AddWithValue("@UserID", userInfo.UserID);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
cmd.ExecuteNonQuery();
con.Close();
return ds;
}
}
}
web.config(客户端)
我的经验:从web.config中删除
-节点。我不知道为什么,但它起作用了。
在IIS 7.5上也有几天了(以前只在IIS 8.0上使用)。jquery AJAX部分在哪里?我从过去3天就被卡住了。尝试了所有可能的解决办法。谁能帮我一下吗?@Ramiz:用jquery AJAX部分编辑。请查看在调试模式下,您是否能够浏览此URL?我没有获得任何数据,也没有收到任何错误:(它只是显示的空白屏幕。但是,当我用普通的C代码调用这个WCF时,我能够执行所有的操作。问题是jQuery。我在配置FIL中犯了一些错误,但是我无法理解它。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace UserDetails1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public DataSet SelectUserDetails()
{
SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=registration;User ID=sa; Password=saviant");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from RegistrationTable", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
cmd.ExecuteNonQuery();
con.Close();
return ds;
}
public string InsertUserDetail(UserDetails userInfo)
{
string Message;
SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=registration;User ID=sa; Password=saviant");
con.Open();
SqlCommand cmd = new SqlCommand("insert into RegistrationTable(UserName,Password,Country,Email) values(@UserName,@Password,@Country,@Email)", con);
cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
cmd.Parameters.AddWithValue("@Password", userInfo.Password);
cmd.Parameters.AddWithValue("@Country", userInfo.Country);
cmd.Parameters.AddWithValue("@Email", userInfo.Email);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
Message = userInfo.UserName + " Details inserted successfully";
}
else
{
Message = userInfo.UserName + " Details not inserted successfully";
}
con.Close();
return Message;
}
public void UpdateRegistrationTable(UserDetails userInfo)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=saviant");
con.Open();
SqlCommand cmd = new SqlCommand("update RegistrationTable set UserName=@UserName,Password=@Password,Country=@Country, Email=@Email where UserID=@UserID", con);
cmd.Parameters.AddWithValue("@UserID", userInfo.UserID);
cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
cmd.Parameters.AddWithValue("@Password", userInfo.Password);
cmd.Parameters.AddWithValue("@Country", userInfo.Country);
cmd.Parameters.AddWithValue("@Email", userInfo.Email);
cmd.ExecuteNonQuery();
con.Close();
}
public bool DeleteUserDetails(UserDetails userInfo)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=saviant");
con.Open();
SqlCommand cmd = new SqlCommand("delete from RegistrationTable where UserID=@UserID", con);
cmd.Parameters.AddWithValue("@UserID", userInfo.UserID);
cmd.ExecuteNonQuery();
con.Close();
return true;
}
public DataSet UpdateUserDetails(UserDetails userInfo)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=saviant");
con.Open();
SqlCommand cmd = new SqlCommand("select * from RegistrationTable where UserID=@UserID", con);
cmd.Parameters.AddWithValue("@UserID", userInfo.UserID);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
cmd.ExecuteNonQuery();
con.Close();
return ds;
}
}
}
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_UserDetails1.Service1" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:50861/Service1.svc" binding="wsHttpBinding"
contract="ServiceReference1.UserDetails1Service1" />
</client>
</system.serviceModel>
</configuration>
function CallService() {
debugger;
$.ajax({
//type: Type, //GET or POST or PUT or DELETE verb
//url: Url, // Location of the service
//data: Data, //Data sent to server
//contentType: ContentType, // content type sent to server
//dataType: DataType, //Expected data format from server
//processdata: varProcessData, //True or False
async: true,
type: "GET",
contentType: "application/json; charset=utf-8",
url: "http://localhost:50861/Service1.svc/SelectUserDetails",
dataType: "json",
crossDomain: true,
// data: '{"teamId":"' + teamId + '", "weekNumber":"' + weekNumber + '"}',
success: function (content) {
debugger;
alert(content);
// DisplayRun(map, content);
},
error: function (msg) {
debugger;
ServiceFailed(msg)// When Service call fails
},
failure: function (msg) {
debugger;
alert("fail" + msg);
}
});
}