2014-10-16 86 views
0

我已經閱讀了很多論壇中有關這些問題的答案,並且鑑於我是這個主題中的一個完整的新手,如果在我的問題中有任何問題,請道歉幼稚。從ASP.NET的jQuery AJAX調用中發佈WCF服務

我有一個ASP.NET的Web應用程序,應調用WCF服務發送和返回[我的地方全部]字符串

我試圖打電話給通過jQuery這種服務,現在我越來越行不通。即使WCF服務在我的本地運行,我也在我的jQuery中找到404-Not Found。

我的web.config文件:

<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors>   
    </behaviors>    
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 
</configuration> 

這是.svc文件

<%@ ServiceHost Language="VB" 
Debug="true" 
Service="TemmeTaskSchedulerService.TemmeTaskSchedulerClass " 
CodeBehind="TemmeTaskScheduler.svc.vb" %> 

接口

<ServiceContract()> 
Public Interface TemmeTaskSchedulerInterface 
    <OperationContract()>    
    Function lstSearchProjects(ByVal environment As String) As String 
End Interface 

實現文件

Public Class TemmeTaskSchedulerClass 
     Implements TemmeTaskSchedulerInterface 

     Public Sub New() 
     End Sub 

      Function lstSearchProjects(ByVal environment As String) As String Implements TemmeTaskSchedulerInterface.lstSearchProjects 
        Return ""success "+environment 
     End Function 
End class 

我的jQuery的電話如下:

$('#environment').change(function() { 
       $.ajax({ 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        url:'http://localhost:60403/TemmeTaskScheduler.svc/lstSearchProjects', 
        data: '{'+'$("#environment").children("option").filter(":selected").text()' + '}',//'{"Username": "' + $("#txtName").val() + '"}', 
        dataType: 'text', 
        processData: false, 
        success: function (data) { 
         alert("success:"+data); 
        }, 
        error: function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); } 

       }) 
      }); 

更正和問題都非常感謝! :)

+0

你在哪裏卡住了?你是否收到任何特定的錯誤信息?如果您將錯誤消息添加到您的問題中,這將非常有幫助。 – Zack 2014-10-16 19:07:37

+0

我從ASP.NET代碼調用服務時收到了404。 – 2014-10-16 19:22:43

+0

當您使用網絡瀏覽器瀏覽到「http:// localhost:60403/TemmeTaskScheduler.svc」時,您仍然會收到404嗎? – Zack 2014-10-16 20:35:37

回答

0

試試這個在WCF的web.config文件

<behaviors> 
    <endpointBehaviors> 
    <behavior name="webScriptEnablingBehavior"> 
     <enableWebScript/> 
    </behavior> 
    </endpointBehaviors> 

    <serviceBehaviors> 
    <behavior name="MyServiceTypeBehaviors"> 
     <serviceMetadata httpGetEnabled="true"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

的信息...去這個鏈接 http://msdn.microsoft.com/en-us/library/vstudio/bb763177(v=vs.100).aspx

+0

,但它們似乎是GET協議jQuery我已經提到它作爲POST。所以這個代碼是相關的 爲了我?我不確定...請爲我確認一次... – 2014-10-16 21:16:50

+0

否.. ,這是針對wcf元數據。以上配置適用於所有http方法。如果您正在使用最新的Visual Studio,則可以選擇創建啓用AJAX的Wcf服務。你可以在web.config中找到唯一的區別「」。 – 2014-10-17 02:44:43