javascript - 如何将从 jquery mobile 表单输入的查询 SQL 发布到 WCF 服务并在 WCF 服务上运行该查询?

标签 javascript wcf wcf-binding

我想要创建一个 jquery 移动表单来从该表单键入 SQL 查询,然后将该查询发布到我的 wcf 服务,并让它将该查询运行到与我的 wcf 服务连接的数据库。但我陷入了我的 wcf 服务中。我创建了一个 javascript 函数来以 json 格式发布查询,并且尝试在 WCF 服务中构建一个方法来接收该查询(以 json 格式),但我的 WCF 服务说不允许该方法.我不知道我的 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>

我的服务契约(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);
} 

我的服务类别:

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>

最佳答案

您的 AJAX 调用正在尝试调用方法“exec”,而您的实际服务方法称为“Execute”。

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

应该是:

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

关于javascript - 如何将从 jquery mobile 表单输入的查询 SQL 发布到 WCF 服务并在 WCF 服务上运行该查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11553356/

相关文章:

c# - WCF 中继已连接到监听器,但没有连接监听器?

WCF:Net.TCP多个绑定(bind),相同端口,不同IP地址

wcf - 本地主机拒绝连接

wcf - 在 impersonate=true 的网站中使用 WCF 的 net.pipe

c# - 在 CheckAccessCore 之后,WCF Access Denied 异常永远不会返回到客户端

JavaScript 正则表达式邮政编码验证问题

javascript - 如何多次顺序执行多个异步函数?

javascript - 用于指令通信的 AngularJS 指令抛出有关未找到 Controller 的错误

c# - 使用 WinForm 或应用程序在客户端上打印

javascript - 从 Jquery 如何确定拆分定界符?