我正在使用jQuery调用web服务,并且我有一个非常奇怪的问题,我整个早上都有问题。当我在开发环境中调用web服务时,一切都运行良好。当投入生产时,我从Firebug得到一个"301永久移动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我可以看到它试图到达我的URL:
http://www.hcemballering.dk/Services/MailService.asmx/SendProductEmail
当手动尝试打开这个URL时,我击中了我的webservice。我也试过改变url,所以它只是使用正常的../Services/MailService。asmx/SendProductEmail。
我也试着看看我的安全设置,它应该工作(所有进程都有访问权)。我甚至尝试给用户"每个人"完全访问"服务",所以这应该不是问题。
这是我的web服务类:
[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;
}
}
任何想法?
好吧,这太愚蠢了。
这是由我的web.config中的规则引起的。我有以下规则:
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false"/>
<action type="Redirect" url="{ToLower:{URL}}"/>
</rule>
当然,我的URL不是小写的。所以我这样做:
<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>
并且所有的东西都是小写的,因为这是一个很好的风格。
它有效!
当您手动访问web服务时,您正在使用GET。ajax使用POST。您的web服务设置为接收POST吗?