それが私のコードであり、.net 4.0 の Web サービスが常に json ではなく xml 形式でデータを返す理由が不思議です。
ウェブサービス :
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public FileModel HelloWorld()
{
FileModel model= new FileModel();
model.Name = "peyman";
return model;
}
jQuery :
次のドロップイベントでファイルリストを取得しvar files = event.originalEvent.dataTransfer.files;
てから呼び出しますupload(files);
function upload(files) {
var data = new FormData();
jQuery.each(files, function(i, file) {
data.append('file-' + i, file);
});
$.ajax({
type : "POST",
url : parameters.Url,
contentType : false,
processData : false,
data : data,
dataType : "json",
success : function(msg) {
alert(msg);
},
error : function() {
alert("err");
}
});
}
また、次のようなことを試しました:
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
Context.Response.ContentType = "application/json";
"application/json"
or"text/json"
を応答ヘッダーに追加する
ただし、応答のコンテンツ タイプは常に"text/xml; charset=utf-8"