如何将用户从jquery移动表单键入的查询SQL发布到WCF服务并在WCF服务上运行该查询?

时间:2012-07-19 03:03:44

标签: javascript wcf wcf-binding

我想创建一个jquery移动表单,从该表单中键入SQL查询,然后将该查询发布到我的wcf serice,让它运行该查询到我的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>

1 个答案:

答案 0 :(得分:2)

当您的实际服务方法被称为“执行”时,您的AJAX调用正在尝试调用方法“exec”。

http://www.greenfields.co.id:502/Service1.svc/exec

应该是:

http://www.greenfields.co.id:502/Service1.svc/Execute