2012-05-30 10 views
8

मैं पिछले 3 घंटों के लिए 100 लिंक के लिए देख रहा हूं उदाहरण के लिए webconfig में scriptfactory जोड़ना , 3 गलतियों, सामग्री प्रकार आदि सेट करनाएएसएमएक्स वेब सेवा जेसन की बजाय एक्सएमएल लौट रही है, सर्विस आउटपुट से <string xmlns = "http://tempuri.org/"> को हटाने की कोशिश कर रहा है

मैं यह समझने में सक्षम नहीं हूं कि वास्तव में गलती क्या है।

पर्यावरण: सेवा .net 4.0

आवश्यकताओं पर चल रहे .net 4.0 वेब अनुप्रयोग पर चल रहा है: मैं ASMX वेब सेवा है कि मुझे एक स्ट्रिंग के रूप एक json लौटा रहा है के साथ एक jqGrid बाध्य करने के लिए की जरूरत है। वेब सेवा फ़ाइल में निम्नलिखित कोड शामिल हैं।

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[ScriptService] 
public class SampleService : System.Web.Services.WebService 
{ 
    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string GetJsonServerProcess() 
    { 
     int memory = 1; 
     string json = string.Empty; 
     var obj = (System.Diagnostics.Process.GetProcesses().Where(r => r.WorkingSet64 > memory).Select(p => new { p.ProcessName, p.WorkingSet64 }).ToArray()); 
     json = Lib.ToJSON(obj); 
     return json; 
    } 
} 

जावास्क्रिप्ट इस प्रकार

<script type="text/javascript"> 
    $(document).ready(function() { 
     jQuery("#jqgajax").jqGrid({ 
      ajaxGridOptions: { type: "POST", contentType: 'application/json; charset=utf-8' }, 
      url:'http://localhost:1092/SampleService.asmx/GetJsonServerProcess', 
      datatype: "json", 
      data: "{}", 
      colNames: ['ProcessName', 'WorkingSet64'], 
      colModel: [ 
         { name: 'ProcessName', index: 'ProcessName', width: 55 }, 
         { name: 'WorkingSet64', index: 'WorkingSet64', width: 90 } 
        ], 
      rowNum: 10, 
      width: 700, 
      rowList: [10, 20, 30], 
      sortname: 'invdate', 
      viewrecords: true, 
      sortorder: "desc", 
      caption: "New API Example" 
     }); 
    }); 
</script> 

एचटीएमएल जब आह्वान बटन पर क्लिक के रूप में

<table id="jqgajax"> 
</table> 
<div id="jqgajax"> 
</div> 

वेब सेवा उत्पादन इस प्रकार है

<string xmlns="http://tempuri.org/"> 
[{"ProcessName":"Dropbox","WorkingSet64":22736896}, 
{"ProcessName":"fdhost","WorkingSet64":1941504}, 
{"ProcessName":"IntelliTrace","WorkingSet64":39276544} 
] 
</string> 

कृपया सुझाव दें कि मुझे क्या याद आ रहा है। <string xmlns="http://tempuri.org/"> टैग मुझे परेशान कर रहे हैं। मुझे लगता है कि ये टैग मेरे ग्रिड को बांधने में सक्षम नहीं हैं।

अद्यतन:

ASMX सेवा अब नीचे के रूप में की तरह दिखता है। आह्वान बटन पर

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[ScriptService] 
public class SampleService : System.Web.Services.WebService 
{ 
    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public List<demo> GetJsonServerProcess() 
    { 
     List<demo> test = new List<demo>(); 

     for(int i=1;i<=10;i++) 
      test.Add(new demo { ProcessName = string.Format("Sample {0}",i), WorkingSet64 = i }); 

     var re = test; 
     return re; 
    } 
} 

public class demo 
{ 
    public string ProcessName { get; set; } 
    public int WorkingSet64 { get; set; } 
} 
+0

संभावित डुप्लिकेट: http: //stackoverflow.com/questions/11088294/asp-net-asmx-web-service-returning-xml-instead-of-json –

+0

प्रश्न समान हैं लेकिन समाधान अलग हैं। लिंक किए गए समाधान में, फिक्स को web.config फ़ाइल को संशोधित करना था, इस समाधान में, फ़िक्स सामग्री-प्रकार शीर्षलेख को संशोधित करना है। हालांकि वे परस्पर अनन्य समाधान नहीं हो सकते हैं। – akousmata

उत्तर

6

क्लिक करना एक्सएमएल रिटर्न क्योंकि अनुरोध contentType: 'application/json; charset=utf-8' निर्दिष्ट नहीं करते। तो Invoke बटन पर क्लिक करने के साथ प्रयोग वास्तव में मदद नहीं करते हैं।

आपके कोड में मुख्य समस्या यह है कि आप डेटा को वेब विधि के अंदर स्ट्रिंग में कनवर्ट करते हैं। लाइन

json = Lib.ToJSON(obj); 

की आवश्यकता नहीं है। ऑब्जेक्ट लौटने पर आम तौर पर क्या होता है। GetJsonServerProcess तरह

[ScriptService] 
public class SampleService : System.Web.Services.WebService 
{ 
    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public List<Process> GetJsonServerProcess() 
    { 
     int memory = 1; 
     return System.Diagnostics.Process.GetProcesses() 
        .Where(r => r.WorkingSet64 > memory) 
        .Select(p => new { p.ProcessName, p.WorkingSet64 }) 
        .ToList(); 
    } 
} 

कुछ को बदला जाना चाहिए अगले समस्या यह है कि डिफ़ॉल्ट इनपुट प्रारूप है जो jqGrid इंतजार एक और है (here देखें)। इसलिए आप jsonReader निर्दिष्ट करने के लिए गुफा करते हैं जो डेटा प्रारूप का वर्णन करते हैं। आपके मामले में यह हो जाएगा की तरह

jsonReader: { 
    repeatitems: false, 
    id: "ProcessName", 
    root: function (obj) { return obj; }, 
    page: function() { return 1; }, 
    total: function() { return 1; }, 
    records: function (obj) { return obj.length; } 
} 

कुछ इसके अतिरिक्त आप अजाक्स url में http://localhost:1092/ उपसर्ग का उपयोग कभी नहीं करना चाहिए क्योंकि आप कैलोरी की वजह से ही सुरक्षा कारणों से एक ही साइट से डेटा प्राप्त। JqGrid में data पैरामीटर का एक और अर्थ jQuery में है, इसलिए आपको data: "{}" को हटा देना चाहिए और ajaxGridOptions से mtype: "POST" पर ले जाना चाहिए।नतीजतन आपके पास

$(document).ready(function() { 
    $("#jqgajax").jqGrid({ 
     mtype: "POST", 
     ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
     url: '/SampleService.asmx/GetJsonServerProcess', 
     postData: "{}", // remove all parameters which jqGrid send typically 
     datatype: "json", 
     colNames: ['ProcessName', 'WorkingSet64'], 
     colModel: [ 
      { name: 'ProcessName', index: 'ProcessName', width: 155 }, 
      { name: 'WorkingSet64', index: 'WorkingSet64', width: 190 } 
     ], 
     jsonReader: { 
      repeatitems: false, 
      id: "ProcessName", 
      root: function (obj) { return obj; }, 
      page: function() { return 1; }, 
      total: function() { return 1; }, 
      records: function (obj) { return obj.length; } 
     }, 
     rowNum: 10, 
     loadonce: true, 
     gridview: true, 
     height: 'auto', 
     rowList: [10, 20, 30], 
     viewrecords: true, 
     sortorder: "desc", 
     caption: "New API Example" 
    }); 
}); 

मुझे कोड का परीक्षण नहीं किया गया है, लेकिन यह आपको आवश्यकतानुसार अधिक निकट होना चाहिए।

अद्यतन: आपको jsonReader बदलकर कोड को ठीक करना चाहिए। आप काम कर रहे डेमो here डाउनलोड कर सकते हैं। यह ग्रिड

enter image description here

मैं सर्वर साइड कोड

using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using System.Web.Services; 

namespace jqGridWebASMX 
{ 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    [System.Web.Script.Services.ScriptService] 
    public class SampleService : WebService 
    { 
     [WebMethod] 
     public List<Demo> GetJsonServerProcess() 
     { 
      const int memory = 1; 
      return Process.GetProcesses() 
       .Where (r => r.WorkingSet64 > memory) 
       .Select(p => new Demo { 
        Id = p.Id, 
        ProcessName = p.ProcessName, 
        WorkingSet64 = p.WorkingSet64 
       }) 
       .ToList(); 
     } 
    } 

    public class Demo 
    { 
     public int Id { get; set; } 
     public string ProcessName { get; set; } 
     public long WorkingSet64 { get; set; } 
    } 
} 

पर और क्लाइंट साइड

$("#list").jqGrid({ 
    mtype: "POST", 
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
    url: '/SampleService.asmx/GetJsonServerProcess', 
    postData: "{}", // remove all parameters which jqGrid send typically 
    datatype: "json", 
    colNames: ['ProcessName', 'WorkingSet64'], 
    colModel: [ 
     { name: 'ProcessName', index: 'ProcessName', width: 200 }, 
     { name: 'WorkingSet64', index: 'WorkingSet64', width: 120, 
      formatter: 'integer', sorttype: 'int', align: 'right' } 
    ], 
    jsonReader: { 
     repeatitems: false, 
     id: "Id", 
     root: function (obj) { return obj.d; }, 
     page: function() { return 1; }, 
     total: function() { return 1; }, 
     records: function (obj) { return obj.d.length; } 
    }, 
    rowNum: 10, 
    loadonce: true, 
    gridview: true, 
    height: 'auto', 
    pager: '#pager', 
    rowList: [10, 20, 30], 
    rownumbers: true, 
    viewrecords: true, 
    sortorder: "desc", 
    caption: "New API Example" 
}); 
$("#pager_left").hide(); // hide unused part of the pager to have more space 
+0

'आपको कभी भी http: // localhost: 1092/अजाक्स url में उपसर्ग का उपयोग नहीं करना चाहिए' वर्तमान में मैंने वेब अनुप्रयोग के लिए सेवाओं के लिए दो अलग-अलग समाधान बनाए हैं। मैंने अभी तक आईआईएस पर सेवा की मेजबानी नहीं की है और परीक्षण उद्देश्य के लिए सीधे इस सेवा का उपयोग किसी अन्य एप्लिकेशन में कर रहा है। मैं jqGrid को लागू करने की कोशिश कर रहा था। तो इस परिदृश्य में, यदि मैं सेवा के लिए पूर्ण पथ का उपयोग कर रहा हूं तो क्या वह काम करेगा? लाइव एप्लिकेशन में मैं यूआरएल का ख्याल रखूंगा। मैं आपके सभी सुझावों का प्रयास करूंगा। लेकिन अब तक यह कठिन समय रहा है। आपके समर्थन के लिए धन्यवाद। –

+0

@ शंतनुगुप्त: यह अजाक्स का सामान्य प्रतिबंध है। प्रतिबंध [उसी मूल नीति] के नाम से जाना जाता है (http://en.wikipedia.org/wiki/Same_origin_policy)। तो आप मेजबान या बंदरगाह के रूप में * अन्य होस्ट या अन्य पोर्ट तक नहीं पहुंच सकते हैं, जिससे आप अनुरोध करते हैं *। उपसर्ग जैसे 'http: // localhost: 1092 /' का कोई मतलब नहीं है। यदि आपको वेब सेवा और समान * वेब सर्वर पर HTML/ASPX पृष्ठ के अनुरूप होना चाहिए। वैकल्पिक रूप से आप JSON के बजाय JSONP का उपयोग कर सकते हैं, लेकिन मामले में उपयोगकर्ता प्रमाणीकरण लागू करने के लिए यह अधिक जटिल है, इसलिए कोई इसे सार्वजनिक वेब सेवाओं के लिए अधिकतर उपयोग करता है। – Oleg

+0

मैं अभी भी डेटा समस्या से जूझ रहा हूं। वेब सेवा जेएसओएन वापस नहीं कर रही है। यह शीर्ष पर एक्सएमएल टैग जोड़ रहा है। '' –

1

ठीक है पर इस्तेमाल किया प्रदर्शित करते हैं, मैं एक ही त्रुटि मिली है और एक के बाद परीक्षण और त्रुटि का भार यहां मेरा "त्वरित और गंदे" समाधान है;

$.get(url, {var1: parameter1, var2: parameter2}, function(data){ 
    data = JSON.parse($(data).find("string").text()); 
    alert("data.source: " + data.source); 
}); 
0
response = await client.GetAsync(RequestUrl, HttpCompletionOption.ResponseContentRead); 
       if (response.IsSuccessStatusCode) 
       { 
        _data = await response.Content.ReadAsStringAsync(); 
        try 
        { 
         XmlDocument _doc = new XmlDocument(); 
         _doc.LoadXml(_data); 
         return Request.CreateResponse(HttpStatusCode.OK, JObject.Parse(_doc.InnerText)); 
        } 
        catch (Exception jex) 
        { 
         return Request.CreateResponse(HttpStatusCode.BadRequest, jex.Message); 
        } 
       } 
       else 
        return Task.FromResult<HttpResponseMessage>(Request.CreateResponse(HttpStatusCode.NotFound)).Result; 
-1

इस कोड का उपयोग मान्य JSON प्रतिक्रिया के लिए ..

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[ScriptService] 
public class SampleService : System.Web.Services.WebService 
{ 
    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public void GetJsonServerProcess() 
    { 
     int memory = 1; 
     string json = string.Empty; 
     var obj = (System.Diagnostics.Process.GetProcesses().Where(r => r.WorkingSet64 > memory).Select(p => new { p.ProcessName, p.WorkingSet64 }).ToArray()); 
     json = Lib.ToJSON(obj); 
     this.Context.Response.ContentType = "application/json; charset=utf-8"; 
      this.Context.Response.Write(json); 

    } 
} 
+0

कृपया लिखें कि आपने क्या किया है, और यह समस्या के लिए कैसे उपयोगी है। –

-1

निम्नलिखित कोड चाल करना चाहिए:

this.Context.Response.ContentType = "application/json; charset=utf-8"; 
this.Context.Response.Write(json); 
-1

इस कोड को पूरी तरह से काम करता है

SqlDataAdapter sda = new SqlDataAdapter(strsql, ConfigurationManager.ConnectionStrings["BTConString"].ToString()); 
DataSet das = new DataSet(); 
sda.Fill(das); 
Context.Response.Output.Write(JsonConvert.SerializeObject(das, Newtonsoft.Json.Formatting.Indented)); 
Context.Response.End(); 

return string.Empty; 
+0

यह स्वीकार्य उत्तर के साथ 5 वर्षीय प्रश्न में कैसे योगदान देता है? .... बहुत ज्यादा नहीं... – VDWWD

संबंधित मुद्दे