我想创建一个 jquery 移动表单以从该表单键入 SQL 查询,然后将该查询发布到我的 wcf 服务,并让它将该查询运行到与我的 wcf 服务连接的数据库。但我陷入了我的 wcf 服务。我创建了一个 javascript 函数来以 json 格式发布我的查询,并且我尝试在我的 WCF 服务中构建一个方法来接收该查询(以 json 格式),但我的 WCF 服务说method not allowed
。我不知道我的 WCF 服务有什么问题。你能帮我在 WCF 服务中创建正确的方法吗?
这是我到目前为止所做的:
我的服务配置:
<system.serviceModel>
<services>
<service name="WcfService.Service1" behaviorConfiguration="ServiceBehaviour">
<endpoint address ="" binding="webHttpBinding" contract="WcfService.IService1" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
我的服务合同:
[ServiceContract]
public interface IService1{
[OperationContract]
//attribute for returning JSON format
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "/Execute")]
//method
void Execute(String query);
}
我的服务等级:
public class Service1 : IService1{
public void Execute(String query){
string connectionString = ConfigurationManager.ConnectionStrings["ConnWf"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString)){
conn.Open();
string cmdStr = String.Format(query);
SqlCommand cmd = new SqlCommand(cmdStr, conn);
SqlDataReader rd = cmd.ExecuteReader();
conn.Close();
}
}
}
我的javascript函数:
function sendToServer(query) {
$.ajax({
beforeSend: function (xhr) {
$.mobile.showPageLoadingMsg();
},
complete: function () {
$.mobile.hidePageLoadingMsg();
},
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://www.greenfields.co.id:502/Service1.svc/Execute",
dataType: "json",
data: JSON.stringify(query),
crossDomain: true,
success:function(){
alert("SUCCESS");
},
error: function () {
alert("ERROR");
}
});
}
这是我的 jquery 移动表单:
<div data-role='page' id='query'>
<div data-theme='a' data-role='header'>
<h3>
Execute Query
</h3>
<a data-inline="true" data-theme="b" href="#menu" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-direction="reverse" data-icon="home" data-transition="slide" data-role="button"><span class="ui-btn-text">Home</span></a>
</div>
<div data-role='content' style='padding: 15px'>
<form class="ui-body ui-body-a ui-corner-all" method="post" action="sendToServer()">
<label for='text_query'>Query:</label>
<textarea name='text_query' id='text_query'></textarea>
<button value="submit-value" name="submit" data-theme="b" type="submit" aria-disabled="false">Execute</button>
</form>
</div>
</div>