为什么这个ajax调用不起作用?当我将其更改为contentType: "application/json; charset=utf-8"
时,其间有分号?
源代码可以通过以下网址进行测试。试着测试一下。
https://jsfiddle.net/rrcjdv6e/7/
$.ajax({
type: "POST",
url: "WebService/HelloWorldWebService.asmx/Test",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#divResponse").show("slow");
$("#divResponse").css("background-color", "green");
$("#divResponse").css("color", "white");
$("#divResponse").css("text-align", "center");
$("#divResponse").css("margin", "20px");
$("#divResponse").text(response.d);
},
failure: function (response) {
alert(response.d);
}
});
[WebMethod]
public string Test()
{
return "Hello World " ;
}
答案 0 :(得分:0)
答案 1 :(得分:0)
只需从Ajax调用中删除contentType
和dataType
属性即可。默认情况下,内容类型应为application/x-www-form-urlencoded
$.ajax({
type: "POST",
url: "WebService/HelloWorldWebService.asmx/Test",
data: {},
success: function (response) {
$("#divResponse").show("slow");
$("#divResponse").css({
"background-color": "green",
"color": "white",
"text-align": "center",
"margin": "20px"
});
$("#divResponse").text(response);
},
failure: function (response) {
alert(response);
}
});