ajax.ajaxmethod无效,jQuery Ajax调用httpget webmethod(C#)无效

小编典典

在我能说的一切之前,您选择的不是最简单的方法。ScriptMethods易于与ASP.NET

ScriptManager一起使用,而不与jQuery一起使用。我建议您最好使用启用JSON的WCF

HTTP服务(最好是RESTfull服务),而不要使用现在尝试使用的ASMX

Web服务。但是,可以使您的代码工作而无需在客户端使用任何Microsoft技术。

首先验证服务器端。

将webmethods.aspx重命名为webmethods.asmx。

确认您放置在\里面,并且配置中还存在一个用于asmx扩展的httpHandlers(ScriptHandlerFactory):

验证是否为从System.Web.Services.WebService继承的类设置了[ScriptService]属性(如果喜欢全名,则为[System.Web.Script.Services.ScriptService])。

现在您可以测试服务了。在您的Web浏览器URL中打开,例如http://localhost/webmethods.asmx/AjaxGet?id =

li1234 如果收到类似

li1234

您可以确定您维修的零件工作正常。

备注: 如果“ Content-Type:application / json;”,则独立于“ ResponseFormat =

System.Web.Script.Services.ResponseFormat.Json”为服务应答赋予XML响应 未在请求中设置。

现在,我们将修复客户端代码。我希望我在以下代码中添加的注释能够全部解释。

再说一句。在代码的最后一部分,我调用了另一个“复杂”的web方法:

[WebMethod]

[ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]

public OutputData AjaxGetMore (InputData input) {

return new OutputData () {

id = input.id,

message = "it's work!",

myInt = input.myInt+1

};

}

哪里

public class OutputData {

public string id { get; set; }

public string message { get; set; }

public int myInt { get; set; }

}

public class InputData {

public string id { get; set; }

public int myInt { get; set; }

}

现在只有在某些地方使用JSON插件的JavaScript代码,如果有人喜欢的话,可以用Crockford的json2.js替换。

var id = "li1234";

// version 1 - works

var idAsJson = '"' + id + '"'; // string serializes in JSON format

$.ajax({

type: "GET",

url: "/webmethods.asmx/AjaxGet?id=" + idAsJson,

contentType: "application/json; charset=utf-8",

success: function(msg) {

alert(msg.d); // var msg = {d: "li1234"}

},

error: function(res, status) {

if (status ==="error") {

// errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace

var errorMessage = $.parseJSON(res.responseText);

alert(errorMessage.Message);

}

}

});

// version 2 with respect of JSON plugin

$.ajax({

type: "GET",

url: "/webmethods.asmx/AjaxGet?id=" + $.toJSON(id),

contentType: "application/json; charset=utf-8",

success: function(msg) {

alert(msg.d); // var msg = {d: "li1234"}

},

error: function(res, status) {

if (status ==="error") {

// errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace

var errorMessage = $.parseJSON(res.responseText);

alert(errorMessage.Message);

}

}

});

// version 3 where jQuery will construct URL for us

$.ajax({

type: "GET",

url: "/webmethods.asmx/AjaxGet",

data: {id: $.toJSON(id)},

dataType: "json",

contentType: "application/json; charset=utf-8",

success: function(msg) {

alert(msg.d); // var msg = {d: "li1234"}

},

error: function(res, status) {

if (status ==="error") {

// errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace

var errorMessage = $.parseJSON(res.responseText);

alert(errorMessage.Message);

}

}

});

// version 4. We set "Content-Type: application/json" about our data, but we use no

// not 'dataType: "json"' parameter. Then we have "Accept: */*" in the request

// instead of "Accept: application/json, text/javascript, */*" before.

// Everithing work OK like before.

$.ajax({

type: "GET",

url: "/webmethods.asmx/AjaxGet",

data: {id: $.toJSON(id)},

contentType: "application/json; charset=utf-8",

success: function(msg) {

alert(msg.d); // var msg = {d: "li1234"}

},

error: function(res, status) {

if (status ==="error") {

// errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace

var errorMessage = $.parseJSON(res.responseText);

alert(errorMessage.Message);

}

}

});

// version 5. If we don't place "Content-Type: application/json" in our reqest we

// receive back XML (!!!) response with "HTTP/1.1 200 OK" header and

// "Content-Type: text/xml; charset=utf-8" which will be placed.

// How one can read in

// http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx),

// ASP.NET AJAX will not make JSON serialized of response data for

// security reasons.

$.ajax({

type: "GET",

url: "/webmethods.asmx/AjaxGet",

data: {id: $.toJSON(id)},

dataType: "json",

//contentType: "application/json; charset=utf-8",

success: function(msg) {

alert(msg.d); // var msg = {d: "li1234"}

},

error: function (res, status, ex) {

// the code here will be works because of error in parsing server response

if (res.status !== 200) { // if not OK

// we receive exception in the next line, be

var errorMessage = $.parseJSON(res.responseText);

alert(errorMessage.Message);

} else {

alert("status=" + status + "\nex=" + ex + "\nres.status=" + res.status + "\nres.statusText=" + res.statusText +

"\nres.responseText=" + res.responseText);

}

}

});

// version 6. Send more komplex data to/from the service

var myData = { id: "li1234", myInt: 100}

$.ajax({

type: "GET",

url: "/webmethods.asmx/AjaxGetMore",

data: {input:$.toJSON(myData)},

dataType: "json",

contentType: "application/json; charset=utf-8",

success: function(msg) {

// var msg = {__type: "Testportal.OutputData", id: "li1234", message: "it's work!", myInt:101}

alert("message=" + msg.d.message + ", id=" + msg.d.id + ", myInt=" + msg.d.myInt);

},

error: function(res, status) {

if (status ==="error") {

// errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace

var errorMessage = $.parseJSON(res.responseText);

alert(errorMessage.Message);

}

}

});

2020-05-19

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值