I've want to create a jquery mobile form to type the SQL query from that form then post that query to my wcf serice and let it run that query to my database that connect with my wcf service. but I get stuck in my wcf service. I've create a javascript function to post my query in json format, and I've try to build a method in my WCF Service to receive that query (in json format) but my WCF Service said method not allowed
. I don't know what's wrong with my WCF Service. Could you help me to create the correct method in WCF Service?
here are what I've made so far:
My service configuration:
<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>
my service contract :
[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);
}
My service class:
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();
}
}
}
My javascript function:
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");
}
});
}
and this is my jquery mobile form:
<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>
Your AJAX call is trying to invoke a method "exec" while your actual service method is called "Execute".
http://www.greenfields.co.id:502/Service1.svc/exec
should be: