0
JsonServiceにXmlHttpRequestを送信する際に問題があります。 私はgoogle chromeを通してリンクに行くとうまくいきます。 このコマンドをchrome(https://i.imgur.com/UxAd8oe.png)に使用すると、このコマンドも機能します。しかし、私はこのようなステップがなくても動作させる必要があります。 これは私のXmlHttpRequestのです:私のサービス・インターフェースにJsonServiceへのXmlHttpRequestに失敗しました
jQuery(document).ready(function() {
$("#btnSendEmail").click(function() {
var message = new Model.Message();
message.From = document.getElementById("txtEmail").value;
message.Subject = "Contact Formulier: " + document.getElementById("txtSubject").value;
message.Html = "Telefoon: " + document.getElementById("txtTelephone").value + "<br/>" +
"Bedrijf: " + document.getElementById("txtCompany").value + "<br/><br/>" +
"Bericht: " + "<br/>" +
document.getElementById("txtMessage").value;
message.Recipients = [];
var recipient = new Model.Recipient();
recipient.Email = "[email protected]";
recipient.Name = "Pieter Borgmans";
recipient.Type = "To";
message.Recipients.push(recipient);
var params = JSON.stringify(message);
var url = "http://localhost:26294/JsonService.svc/Test";
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.contentType = "application/json; charset=utf-8";
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function() {
if (this.status === 200) {
var response = (this.responseText) ? JSON.parse(this.responseText) : this.responseText;
$("#lblSendMailMessage").text = response;
} else {
$("#lblSendMailMessage").text = xhr.status + ": " + xhr.statusText;
}
};
xhr.onerror = function (onErrorArgs) {
$("#lblSendMailMessage").text = "Error";
};
xhr.send();
});
});
コード:
[ServiceContract]
public partial interface IBusinessManager
{
[OperationContract]
[FaultContract(typeof (ServiceFault))]
[WebGet(
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "Test")]
string Test();
}
そして、私は私のweb.configファイルにこのコードを追加:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Accept, Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control" />
<add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS, TRACE, HEAD" />
<add name="Access-Control-Max-Age" value="1000" />
</customHeaders>
</httpProtocol>
誰もが持ってアイデア?
ありがとうございます!
正確には期待どおりに機能しません。どのように動作しますか? – victor175
既にjQueryを使用しているので、なぜあなたは 'XmlHttpRequest'を直接使用していますか? [jQuery.get()](https://api.jquery.com/get/)を使用して、多くの問題を解消し、簡単なデバッグのために成功/エラーハンドラをフックできます。 –
これは私が受け取っているエラーメッセージです:XMLHttpRequestはhttp:// localhost:26294/JsonService.svc/Testをロードできません。プリフライトの応答に無効なHTTPステータスコード405があります [ネットワーク]タブを確認すると、これが私のリクエストに表示されています。 リクエストURL:http:// localhost:26294/JsonService.svc/Test リクエスト方法:オプション ステータスコード:405メソッドが許可されていません リモートアドレス:[:: 1]:26294 ご覧のとおり、エラーが発生していますか、または何か別の意味ですか? –