jQuery AJAX呼び出しで利用されるASP.net Webサービスを作成しようとしています。 私はこれをうまく働かせようとしています。私はオンラインで多くの類似の質問を見てきましたが、私はそれに合った解決策を見つけることができませんでした。ASP.net JSON Webサービスの問題の応答タイプ
jqueryを使用してajax呼び出しを試みると、サーバーから正常に応答しますが、パーサーエラーが原因で要求が失敗します。
ウェブサービスから返されたjsonを検証しました。有効です。この問題は、asp.netがjsonオブジェクトをxmlとして返すという事実に由来しているようです。
私は、コンテンツタイプに設定されて<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>
それは潜在的な修正として言及されたように、私は次のHTTPハンドラを追加しました
<Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
を使用してJSONとして戻り値の型を指定した
jquery ajaxの設定で "application/json; charset = utf-8"とdataTypeを "json"に設定します。要求のタイプは正しいと思われますが、応答は常にxmlです。
私はdataTypeを削除することによって正常にリクエストを行うことができますが、私は非常にevalを使用してデータを逆シリアル化することを避けたいと思います。
誰かが何か提案があれば、大変感謝します。私はこれについて数日間、私の髪を引っ張ってきました。
JAVASCRIPT
(function($) {
$.ajaxSetup({
type: "POST",
contentType: "application/json; charset=utf-8",
global: false,
dataType: "json"
});
function auto() { console.log("test"); return; };
$.fn.species = {
test: function() { alert("test"); },
load: function() { //load and attach species list to element as dropdown
$.ajax({
url: "SpeciesService.asmx/List",
success: function(msg) { console.log(msg); },
error: function (xhr, desc, exceptionobj) {
console.log(xhr.responseText);
console.log(desc);
console.log(exceptionobj);
}
});
return this;
}
}; //Species Block
})(jQueryの)。 // jQueryのエイリアスブロック
ASP.NET Webサービス
<%@ WebService Language="VB" Class="SpeciesService" %>
輸入System.Webの インポートSystem.Web.Services 輸入System.Web.Services.Protocols 輸入種 輸入System.Runtime.Serialization
'このWebサービスをスクリプトから呼び出すには、ASP.NET AJAXを使用するには、次の行のコメントを外します。 '_
_ _ パブリック・クラスSpeciesService
Inherits System.Web.Services.WebService
<WebMethod()> _
<Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
Public Function Data(ByVal id As Integer) As String
Dim curSpecies As New Species(id)
Return curSpecies.serialize
End Function
<WebMethod()> _
<Script.Services.ScriptMethod(responseFormat:=Script.Services.ResponseFormat.Json)> _
Public Function List() As String
Return Species.list()
End Function
エンドクラス
この問題はjQuery 1.4.xで修正されていると思います。 jQuery 1.3.xでは、必要なのはデータだけです: '{}'。 *どんな*データでも、jQueryはコンテンツタイプを強制的に設定します。正当なキーと値は必要ありません。 –