2010-07-29 52 views
0

它又是我(previous question)我仍然有json和xml从ajax调用返回的问题。Ajax查询到我的web服务返回xml在我的json - 第2部分

我在MonoDevelop 2.2版中写了一个web服务来返回我的json。

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string getLocationJ(){} 

将返回: -

JavaScriptSerializer js = new JavaScriptSerializer(); 
string json = js.Serialize(bigPM); 
return json; 

如果我测试我的web服务,我得到: -

<?xml version="1.0" encoding="utf-8"?> 
<string xmlns="http://tempuri.org/">[{"placeName":"XXXX","address":"Some Address","lat":12121,"lng":12121}]</string> 

这正是我在拉的时候我让我的Ajax调用。我的json仍然包装在XML中,因此无法读取。

这是我的AJAX调用: -

$.ajax({ 
    type: "GET", 
    url: theURL, 
    async: true, 
    data: {minLong:minLon, maxLong:maxLon, minLat:minLat, maxLat:maxLat}, 
    cache: false, 
    dataType: "jsonp", 
    contentType: "application/xml; charset=utf-8", 
    success: function (data) { 
    alert('in here'); 



    }, 
    error:function (xhr, ajaxOptions, thrownError){ 
       alert(xhr.status); 
       alert(thrownError); 
       alert(xhr.statusText); 
      } 
    }); 

如果我不只是JSON我获得500内部服务器错误,如果我做一个职位,我得到403 Forbidden错误。

今天早上,我试着这样做: -

$.getJSON(theURL, {minLong:minLon, maxLong:maxLon, minLat:minLat, maxLat:maxLat}, function(data) { 
); 
}); 

只有我得到确切同样的问题。

如果我可以从我的json中删除xml,那么我可以前进,但现在我死在水中,我想我在ajax中溺水了!

请帮 谢丽尔

+1

这不是一个JavaScript的问题。你的服务器是将XML包装器放在字符串的周围,所以这是某种配置问题。 – Pointy 2010-07-29 14:20:03

+0

我觉得有趣的是,我正在关注这个例子 http://williamsportwebdeveloper.com/cgi/wp/?p=494 如果你测试他的web服务,他们都返回包装在XML中的json。但他也在他的ajax电话中做了一个'POST'。也许我也应该做一个POST。但有了POST,我得到了403个禁止。你认为这是由于我打电话给HTTPS吗?我怎样才能解决403错误?有任何想法吗? – Cheryl 2010-07-29 14:52:51

回答

0

变化

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

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

完整的示例:

/* in this case I am using */ 
     <script src="Js/json2.js" type="text/javascript"></script> 

    // available at: http://www.json.org/js.html 

function jsonObject() 
{ 
}; 
var phoneListObject = new jsonObject(); 

function SaveJsonObject() 
{ 
    phoneListObject.Control = new jsonObject(); 
    phoneListObject.Control.CustomerId = $("#CustomerId").val(); 
    phoneListObject.Control.CustomerName = $("#CustomerName").val(); 
    phoneListObject.ListBody.PhonesBlock = new jsonObject(); 
    phoneListObject.ListBody.PhonesBlock.Phone = new Array(); 
    $('#PhonesBlock .Phone').each(function(myindex) 
    { 
     phoneListObject.ListBody.PhonesBlock.Phone[myindex].PhoneNumber = $(".PhoneNumber input", this).val(); 
     phoneListObject.ListBody.PhonesBlock.Phone[myindex].PhoneName = $(".PhoneName input", this).val(); 
    }); 
}; 

$(function() 
{ 
    function SaveCurrentList() 
    { 
     SaveJsonObject(); 
     var currentSet = phoneListObject; 
     var formData = { FormData: currentSet }; 
     phoneListJSON = JSON.stringify(formData); 
     var FormData = "{ FormData:" + JSON.stringify(phoneListJSON) + "}"; 
     SavePhoneListData(FormData); 
    }; 
    function SavePhoneListData(phonesData) 
    { 
     $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      data: phonesData, 
      dataFilter: function(data) 
      { 
       var msg; 
       if ((typeof (JSON) !== 'undefined') && 
     (typeof (JSON.parse) === 'function')) 
        msg = JSON.parse(data); 
       else 
        msg = eval('(' + data + ')'); 
       if (msg.hasOwnProperty('d')) 
        return msg.d; 
       else 
        return msg; 
      }, 
      url: "../../WebServices/ManagePhones.asmx/SaveJson", 
      success: function(msg) 
      { 
       SaveSuccess(msg);/* the JSON is in the msg, create this function to do what you want with it. */ 
      }, 
      complete: function(xhr, textresponse) 
      { 
       var err = eval("(" + xhr.responseText + ")"); 
      }, 
      error: function(msg) 
      { 
      }, 
      failure: function(msg) 
      { 
      } 
     }); 
    }; 
    $('#btnSave').click(function() 
    { 
     SaveCurrentList(); 
    }); 
}); 

/* json data snip */ 
{"FormData":{"Control":{"CustomerId":"12345y6","CustomerName":"Joe Customer"},"PhonesBlock":{"Phone":[{"PhoneNumber":"234-233-2322","PhoneName":"son harry"},{"PhoneNumber":"234-233-2323","PhoneName":"son frank"},{"PhoneNumber":"234-233-2320","PhoneName":"momk"}]}}} 

/*XML of the form data:*/ 
<FormData> 
    <Control> 
     <CustomerId>12345y6</CustomerId> 
     <CustomerName>Joe Customer</CustomerName> 
    </Control> 
    <PhonesBlock> 
     <Phone> 
      <PhoneNumber>234-233-2322</PhoneNumber> 
      <PhoneName>son harry</PhoneName> 
     </Phone> 
     <Phone> 
      <PhoneNumber>234-233-2323</PhoneNumber> 
      <PhoneName>son frank</PhoneName> 
     </Phone> 
     <Phone> 
      <PhoneNumber>234-233-2321</PhoneNumber> 
      <PhoneName>momk</PhoneName> 
     </Phone> 
    </PhonesBlock> 
</FormData> 

/* form layout snip */ 

<div class="control"> 
    <div class="customer"> 
     <input typeof="text" id="CutomerId" /> 
     <input typeof="text" id="CutomerName" /> 
    </div> 
    <div class="phoneslist" id="PhonesBlock"> 
     <div class="Phone"> 
      <input typeof="text" class="PhoneNumber" /> 
      <input typeof="text" class="PhoneName" /> 
     </div> 
     <div class="Phone"> 
      <input typeof="text" class="PhoneNumber" /> 
      <input typeof="text" class="PhoneName" /> 
     </div> 
     <div class="Phone"> 
      <input typeof="text" class="PhoneNumber" /> 
      <input typeof="text" class="PhoneName" /> 
     </div> 
    </div> 
</div> 
<input id="buttonSave" class="myButton" type="button" value="Save" /> 

Web服务方法的签名:

[WebMethod(EnableSession = true)] 
    public string SaveJson(string FormData) 
    { 
    } 
+0

ooops太糟糕了我误以为 - 我已经这样做了,它没有什么区别我仍然有我的json包裹在xml中。 – Cheryl 2010-07-29 14:28:19

+0

你可以尝试使用从以下序列化程序类:http://james.newtonking.com/pages/json-net.aspx – 2010-07-29 14:30:03

+0

是否我做json或xml我一直在萤火虫中获取此响应: - 失踪;语句前 [{... nue“,”lat“:-79.45,”lng“:43.6801},{”pla – Cheryl 2010-07-29 14:38:50

0

一个快速而脏的修复方法是从成功函数中的xml中提取你的json。

$.ajax({ 
    type: "GET", 
    url: theURL, 
    async: true, 
    data: {minLong:minLon, maxLong:maxLon, minLat:minLat, maxLat:maxLat}, 
    cache: false, 
    dataType: "jsonp", 
    contentType: "application/xml; charset=utf-8", 
    success: function (data) { 

data = extractJsonFromXml(data); 
    //you have to write extractJsonFromXml function in js, you could use substring, or a regex replace. 

} 
0

确保您的服务类具有[ScriptService]属性。该属性默认不添加。

0

而是在你的WebMethod返回一个字符串,返回void及用途:

JavaScriptSerializer js = new JavaScriptSerializer(); 
Context.Response.Write(js.Serialize(YOUR_STRING_TO_OUTPUT)); 
+0

请不要复制并粘贴您的答案以解决多个问题。如果问题不同,请将您的答案与所问的问题区分开来。如果问题是相同的,则应该标记或关闭(您还没有声誉)。 – 2013-05-16 01:25:58