2010-04-16 11 views
12

मैं एक AJAX को पीछे कोड में वेबमाइंड प्राप्त करने का प्रयास कर रहा हूं। समस्या यह है कि मैं jQuery onfail विधि से त्रुटि "पार्सरर" प्राप्त कर रहा हूं।JQuery AJAX कॉल httpget webmethod (C#) पर काम नहीं कर रहा है

यदि मैं एक पोस्ट में जीईटी बदलता हूं तो सब ठीक काम करता है। कृपया नीचे मेरा कोड देखें।

अजाक्स कॉल

<script type="text/javascript"> 
     var id = "li1234"; 

     function AjaxGet() { 
      $.ajax({ 
       type: "GET", 
       url: "webmethods.aspx/AjaxGet", 
       data: "{ 'id' : '" + id + "'}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       async: false, 
       success: function(msg) { 
        alert("success"); 

       }, 
       error: function(msg, text) { 
        alert(text); 
       } 
      }); 
     } 

    </script> 

कोड के पीछे

[System.Web.Services.WebMethod] 
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true, 
    ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)] 
public static string AjaxGet(string id) 
{ 
    return id; 
} 

Web.config

 <webServices> 
      <protocols> 
       <add name="HttpGet"/> 
      </protocols> 
     </webServices> 

यूआरएल इस्तेमाल किया जा रहा

......../webmethods.aspx/AjaxGet {% 20% 27id% 27% 20% 20% 27li1234% 27}?

प्रतिक्रिया के हिस्से के रूप में यह पृष्ठ वेबमाइड के लिए एचटीएमएल वापस कर रहा है।

किसी भी मदद की सराहना की जाएगी।

उत्तर

22

सब इससे पहले कि मैं कह सकते हैं, कि आप सबसे आसान तरीका नहीं चुनें। ScriptMethods को ASP.NET ScriptManager के साथ उपयोग करना आसान है और jQuery के साथ नहीं। मैं आपको सलाह देता हूं कि आप अब उपयोग करने का प्रयास करने वाले एएसएमएक्स वेबसाइट सेवा के बजाय जेएसओएन-सक्षम डब्ल्यूसीएफ HTTP सेवाओं (रीस्टफुल सेवा के रूप में बेहतर) का बेहतर उपयोग करें। फिर भी, कोई आपको क्लाइंट पक्ष पर किसी भी Microsoft तकनीक का उपयोग किये बिना कोड को काम कर सकता है।

सबसे पहले सर्वर की तरफ सत्यापित करें।

  1. webmethods.aspx पर webmethods.aspx का नाम बदलें।
  2. सत्यापित करें कि आप \ के अंदर रखा जाता है और 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> 
    
  3. सत्यापित करें कि [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); 
     } 
    } 
}); 
+0

इस समाधान का आपका समाधान 'asmx' वेब सेवाओं के लिए काम करता है, लेकिन क्या यह काम करना संभव है यदि WebMethod 'aspx' फ़ाइल के अंदर है? –

+0

@ मैट रॉबर्ट्स: सबकुछ संभव है, लेकिन मैं इसका कारण समझ नहीं पा रहा हूं। एएसपीएक्स सामान्य एचटीएमएल पेजर्स या वेब फॉर्म के लिए उपयोग किया जाता है। तो * संबंधित हैंडलर * यूआरएल के अनुरोध को संसाधित करने के लिए इस्तेमाल किया जाएगा। इसलिए फ़ाइल एक्सटेंशन का उपयोग वास्तव में व्यावहारिक है। आप [ASHX] (http://msdn.microsoft.com/en-us/library/bb398986 (v = vs.100) .aspx) का उपयोग कर सकते हैं सामान्य हैंडल बनाने के लिए जो सामान्य HTTP अनुरोध को संसाधित करते हैं और जिनके पास UI नहीं है या एचटीएमएल मार्कअप। – Oleg

2
//... 
data: { "id" : id }, 
//... 

डेटा एक ऑब्जेक्ट है, एक स्ट्रिंग जो ऑब्जेक्ट की तरह दिखता है।

आप एक स्ट्रिंग का उपयोग करते हैं, यह सही स्वरूप वाला URL क्वेरी स्ट्रिंग, इस तरह होना चाहिए:

//... 
data: "id=" + encodeURIComponent(id) + "&otherstuff=" + encodeURIComponent(data), 
//... 
+0

समझ में आता है कि। अब मेरे पास यह यूआरएल है ...../webmethods.aspx/AjaxGet? Id = li1234 लेकिन यह अभी भी एक ही त्रुटि, "parseerror" लौट रहा है। –

+0

@ टिम: ओह, अब मैं इसे देखता हूं। आपके जीईटी अनुरोध का सामग्री-प्रकार जेएसओएन नहीं है। बस उस पैरामीटर को पूरी तरह से छोड़ दें। प्रतिक्रिया प्रकार JSON है, इसलिए डेटा टाइप पैरामीटर ठीक है। – Tomalak

+0

धन्यवाद, अब मुझे कोई त्रुटि नहीं मिल रही है, हालांकि यह मेरा वेबमाइट मार नहीं रहा है, भले ही यह सफलता समारोह में लौट रहा हो। प्रतिक्रिया में मैं सिर्फ वेबविड्थ से एचटीएमएल प्राप्त करता हूं। दर्द होने के लिए खेद है, कृपया मदद करें। –

1

तुम भी http://www.json.org/js.html JSON.stringify जहां यह पैरामीटर के रूप में एक json वस्तु स्वीकार करता है और एक स्ट्रिंग रिटर्न की जाँच कर सकते हैं।

2

मैं यहां जवाब की तलाश में आया ... दूसरों के लिए, यहां जवाब है।

सुरक्षा कारणों से, एएसपी.Net AJAX पृष्ठ विधियां केवल POST अनुरोधों का समर्थन करती हैं।

(https://stackoverflow.com/a/2397521 से)

0

वीबी का उपयोग करने वालों के लिए, इस तरह अपने विधि को सजाने:

<WebMethod()> 
<ScriptMethod(UseHttpGet:= True, ResponseFormat:= ResponseFormat.Json)> 
संबंधित मुद्दे