2012-02-27 145 views
2

我有以下的asmx服務調用ASMX服務從JavaScript

[WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService] 

    public class DinnerService : System.Web.Services.WebService 
    { 
     List<Dinner> dinners; 
     public DinnerService() 
     { 
      dinners = new List<Dinner>(); 
     } 

     [WebMethod] 
     [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] 
     public Dinner HelloWorld() 
     { 
      var dinner = new Dinner { DinnerID = 1, Host = "Ahsan", RSVP = "Some People", Venue = "Lahore" }; 
      return dinner; 
     } 
} 

和IM從jQuery的調用它在網頁表單的頁面加載事件如下

$(function() { 
       $.ajax({ 
        type: 'post', 
        url: 'http://localhost:1901/DinnerService.asmx/HelloWorld', 
        dataType: 'json', 
        data:{}, 
        contentType: "application/json; charset=utf-8", 
        success: function (data) { 
         $('#res').html(data.d.DinnerID); 
         alert(data.d.Host); 
        } 
       }); 
}); 

它工作正常,在IE瀏覽器,但在Firefox和鉻它顯示內部服務器錯誤和響應就像

Request format is unrecognized for URL unexpectedly ending in '/HelloWorld' 

這對我來說很奇怪w它在一個瀏覽器上工作,而不在其他人上工作。我正在使用Visual Studio 2010和.NET 3.5進行該服務。對於這個特殊情況,去WCF不是選項。互聯網上的迴應無法幫助我解決問題。任何人都可以解釋究竟發生了什麼嗎?

+0

您是否試過這種解決方案? http://stackoverflow.com/questions/657313/request-format-is-unrecognized-for-url-unexpectedly-ending-in – 2012-02-27 11:01:49

+0

有很多問題與網絡上類似的標題,但他們不會遇到問題在一個特定瀏覽器。此外,我在服務項目的web.config中找不到''部分 – 2012-02-27 11:04:41

+0

如果您的web.config中沒有部分,只需在部分下添加一個部分即可。 – jmaglio 2012-02-27 14:56:26

回答

1

以下設置到您的web.config文件將解決此問題。 查看follownig的帖子。

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