Web服务在生产环境中返回“永久移动301错误”

时间:2013-03-23 13:34:53

标签: jquery web-services iis relative-path

我使用jQuery调用一个Web服务,我有一个非常奇怪的问题,我整个上午都遇到了问题。当我在开发环境中调用Web服务时,一切都运行良好。投入生产后,我从Firebug获得了“301 Moved Permanently 38ms”。

我的脚本是这样的:

 var data = '{"product":"' + productName + '", "from":"' + from + '", "question":"' +     question + '", "phone":"' + phone + '", "type":"' + typeOfMail + '"}';
$.ajax({
    type: "POST",
    datatype: "json",
    data: data,
    url: '<%= Page.ResolveUrl("~/Services/MailService.asmx/SendProductEmail") %>',
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        resetContactControls();
        $('#<%=AskQuestionProductBtn.ClientID %>').hide();
    },
    failure: function (data) {
    }
});

这将编译为生产中的以下URL:

url: '/Services/MailService.asmx/SendProductEmail'

在我的生产环境中,使用Firebug,我可以看到它尝试访问我的网址:

http://www.hcemballering.dk/Services/MailService.asmx/SendProductEmail

手动尝试打开此网址时,我点击了我的网络服务。我也尝试更改网址,以便它只使用正常的../Services/MailService.asmx/SendProductEmail。

我还尝试查看我的安全设置,它应该可以工作(所有进程都可以访问)。我甚至试图让用户“Everyone”完全访问“服务”,所以这应该不是问题。

这是我的网络服务类:

[ScriptService]
public class MailService : System.Web.Services.WebService {

    ILog logger = LogManager.GetLogger(typeof(MailService));

    [WebMethod]
    public bool SendProductEmail(string product, string from, string question, string phone, string type)
    {
        try
        {

            StringBuilder content = new StringBuilder();

            content.AppendLine(
                string.Format(
                    "Produkt:<br/>{0}<br/><br/>Fra email:<br/>{1}<br/><br/>Telefon:<br/>{2}<br/><br/>Type af henvendelse:<br/>{3}<br/><br/>Spørgsmål:<br/>{4}",
                    product, from, phone, type, question));
            var module = new MailModule(content.ToString(), "Kontakt om HC produkt: " + product);
            module.SendMail();
        }
        catch (Exception exp)
        {

            throw new Exception("Mailen blev desværre ikke sendt, da der skete en fejl");
        }


        return true;
    }

}

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

好的,这太傻了。

这是由我的web.config中的规则引起的。我有以下规则:

  <rule name="LowerCaseRule1" stopProcessing="true">
          <match url="[A-Z]" ignoreCase="false"/>

          <action type="Redirect" url="{ToLower:{URL}}"/>
        </rule>

当然,我的网址并非小写。所以我这样做了:

 <rule name="LowerCaseRule1" stopProcessing="true">
          <match url="[A-Z]" ignoreCase="false"/>
          <conditions>
            <add input="{URL}" matchType="Pattern" pattern="^.+\.((axd)|(js)|(xaml)|(asmx))$" ignoreCase="true" negate="true"/>
          </conditions>
          <action type="Redirect" url="{ToLower:{URL}}"/>
        </rule>

并且还把所有东西都做成小写,只因为它的风格很好。

它有效!

答案 1 :(得分:0)

手动点击网络服务时,您正在使用GET。你ajax正在使用POST。您的Web服务是否设置为接收POST?