Reputation: 9996
I am calling a web service using jQuery, and I have a very strange problem i've had problems with all morning. When I call the web service in my development environment, everything works perfectly. When put into production, I get an "301 Moved Permanently 38ms" from Firebug.
I have my script which is like this:
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) {
}
});
This compiles to the following URL in production:
url: '/Services/MailService.asmx/SendProductEmail'
In my production environment, with Firebug I can see it tries to reach my URL:
http://www.hcemballering.dk/Services/MailService.asmx/SendProductEmail
When manually trying to open this URL, I hit my webservice. I've also tried changing the url so it just uses the normal ../Services/MailService.asmx/SendProductEmail .
I've also tried to look at my security settings, and it should work (all processes have access). I even tried to give the user "Everyone" full access to "Services", so it shouldn't be the problem.
This is my web service class:
[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;
}
}
Any ideas?
Upvotes: 0
Views: 4751
Reputation: 9996
Ok, this was silly.
This was caused by a rule in my web.config. I had the following rule:
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false"/>
<action type="Redirect" url="{ToLower:{URL}}"/>
</rule>
And of course, my URL wasn't lower case. So I did this:
<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>
And also made everything lowercase, just because it's good style anyway.
And it works!
Upvotes: 3
Reputation: 11444
You're using GET when you manually hit the web service. You ajax is using POST. Is your web service set up to receive POST?
Upvotes: 0