सब इससे पहले कि मैं कह सकते हैं, कि आप सबसे आसान तरीका नहीं चुनें। ScriptMethods को ASP.NET ScriptManager के साथ उपयोग करना आसान है और jQuery के साथ नहीं। मैं आपको सलाह देता हूं कि आप अब उपयोग करने का प्रयास करने वाले एएसएमएक्स वेबसाइट सेवा के बजाय जेएसओएन-सक्षम डब्ल्यूसीएफ HTTP सेवाओं (रीस्टफुल सेवा के रूप में बेहतर) का बेहतर उपयोग करें। फिर भी, कोई आपको क्लाइंट पक्ष पर किसी भी Microsoft तकनीक का उपयोग किये बिना कोड को काम कर सकता है।
सबसे पहले सर्वर की तरफ सत्यापित करें।
- webmethods.aspx पर webmethods.aspx का नाम बदलें।
सत्यापित करें कि आप \ के अंदर रखा जाता है और config में एक ASMX विस्तार (ScriptHandlerFactory) के लिए httpHandlers भी मौजूद हैं:
<configuration>
<!-- ... -->
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
<httpHandlers>
<!-- ... -->
<add verb="*" path="*.asmx"
type="System.Web.Script.Services.ScriptHandlerFactory"
validate="false"/>
</httpHandlers></system.web></configuration>
सत्यापित करें कि [ScriptService] विशेषता ([System.Web.Script.Services। स्क्रिप्ट सेवा] यदि आपको पूर्ण नाम पसंद हैं) System.Web.Services.WebService से विरासत में प्राप्त आपकी कक्षा के लिए सेट करें।
अब आप सेवा का परीक्षण कर सकते हैं।http://localhost/webmethods.asmx/AjaxGet?id=li1234 की तरह आप वेब ब्राउज़र URL में खोलें तुम वापस की तरह
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">li1234</string>
कुछ प्राप्त होता है आपको यह सुनिश्चित करना है कि आप सेवा हिस्सा ठीक काम करता है हो सकता है।
टिप्पणी: Independ पर "ResponseFormat = System.Web.Script.Services.ResponseFormat.Json" एक्सएमएल प्रतिक्रियाओं के साथ सेवा जवाब का श्रेय अगर "सामग्री प्रकार: आवेदन/json," अनुरोध में सेट नहीं।
अब हम ग्राहक कोड को ठीक करेंगे। मुझे उम्मीद है कि मैंने जो कोड निम्नलिखित कोड में रखा है, वह सभी को समझाएं।
एक और छोटी टिप्पणी। कोड के अंतिम भाग में मैं एक और "जटिल" वेब विधि कॉल:
[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 प्लगइन है, जो 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);
}
}
});
इस समाधान का आपका समाधान 'asmx' वेब सेवाओं के लिए काम करता है, लेकिन क्या यह काम करना संभव है यदि WebMethod 'aspx' फ़ाइल के अंदर है? –
@ मैट रॉबर्ट्स: सबकुछ संभव है, लेकिन मैं इसका कारण समझ नहीं पा रहा हूं। एएसपीएक्स सामान्य एचटीएमएल पेजर्स या वेब फॉर्म के लिए उपयोग किया जाता है। तो * संबंधित हैंडलर * यूआरएल के अनुरोध को संसाधित करने के लिए इस्तेमाल किया जाएगा। इसलिए फ़ाइल एक्सटेंशन का उपयोग वास्तव में व्यावहारिक है। आप [ASHX] (http://msdn.microsoft.com/en-us/library/bb398986 (v = vs.100) .aspx) का उपयोग कर सकते हैं सामान्य हैंडल बनाने के लिए जो सामान्य HTTP अनुरोध को संसाधित करते हैं और जिनके पास UI नहीं है या एचटीएमएल मार्कअप। – Oleg