日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2024/9/3 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的ajax.ajaxmethod无效,jQuery Ajax调用httpget webmethod(C#)无效的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。