2012-09-13 75 views
0

我運行的asmx服務中的一個端口本地主機:5739,並試圖從其他端口撥打服務或普通的HTML + jQuery的出項目的無法通過jQuery來調用ASMX Web服務的Ajax

,但我不能訪問Web服務

我的web服務是

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string HelloWorld(string text) { 
    return "Hello World" + text; 
} 

和我的代碼來調用Web服務是

  $.ajax(
      { 
       type: "POST", 
       url: "http://localhost:5739/asmxservices/testservice.asmx/HelloWorld", 
       data: '{ "text": "Kartheek"}',      
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: OnSuccess, 
       error: OnError 

      }); 

      function OnSuccess(data, status) { 
       alert(data.d); 

      }; 
      function OnError(msg) { 
       alert('error = ' + msg.d); 
      } 
+0

什麼是錯誤?錯誤的格式,404,?可能重複http://stackoverflow.com/questions/811353/escaped-json-response-with-webmethod – Cybermaxs

+0

總是會進入OnError(msg)函數。並顯示未定義從msg.d – Kartheek

回答

1

你的方法必須是靜態的,就像這樣:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public static string HelloWorld(string text) { 
    return "Hello World" + text; 
} 
0

看來你的麻煩所造成的文件放置。 致電您的服務的JS代碼與您的服務位於同一主機上。 只需將html文件放在項目根文件夾中,並嘗試在調試時從目錄列表中運行它。 作爲樣本:我測試與路徑

http://localhost:51169/2.html

0

以下步驟解決我的問題,服務調用的文件,希望它可以幫助一些一,

  1. 要允許此Web服務從腳本調用,使用ASP.NET AJAX,在asmx服務類上面包含以下行,例如

[System.Web.Script.Services.ScriptService] public class GetData:System.Web.Serv ices.WebService {

  • 添加協議下的System.Web在web.config中,請點擊鏈接,如果你不能夠查看配置,
  • https://pastebin.com/CbhjsXZj

    <system.web> 
    <webServices> 
        <protocols> 
        <add name="HttpGet"/> 
        <add name="HttpPost"/> 
        </protocols> 
    </webServices>