簡體   English   中英

ASMX Web服務不返回JSON,只能使用application / x-www-form-urlencoded contentType進行POST

[英]ASMX webservice not returning JSON, can only POST using application/x-www-form-urlencoded contentType

我可以使用jQuery調用我的Web服務,如果contentType =“ application / x-www-form-urlencoded; charset = utf-8”

但是,這將返回xml: <string>[myjson]</string>

如果嘗試使用“ application / json; charset = utf-8”發布到服務,則會收到500錯誤,其中包含空StackTrace和ExceptionType。 我的Web服務功能從未被使用過,因此我不確定如何調試這種情況。

我的方法和類都裝飾有適當的屬性,並設置為使用JSON作為其響應類型(我的wsdl和disco文件也是如此)。 我已經安裝了Ajax擴展,並且在web.config中有必需的條目。

這是在SharePoint服務器場上,但是我不確定這有什么太大的不同。 我在所有WFE上部署了web.config更改,並安裝了ajax擴展。 服務再次起作用,它只接受默認內容類型,什么都不接受。

不知道我在這里想念的是什么,伙計們...

我的ajax電話:

$.ajax({
type: "POST",
url: "/_vti_bin/calendar.asmx/Test",
dataType: "json",
data: "{}",
contentType: "application/json; charset=UTF-8",
success: function(msg){
    alert(msg);
    },
error: function(xhr, msg){ alert(msg + '\n' + xhr.responseText); }
});

我的網絡服務課程:

[WebService(Namespace = "http://namespace")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class CalendarService : WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Test()
    {
        return "Hello World";
    }
}

我已經使用Web服務在2.0中進行了此工作,但是我已經對.d進行了保護(請參見下面的dataFilter)。 我還返回了一個對象數組。 注意:該對象的類是靜態的,否則至少對我而言無法正常工作。

  $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        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: "webservice/ModifierCodesService.asmx/GetModifierList",
        success: function(msg)
        {
            LoadModifiers(msg);
        },
        failure: function(msg)
        {
            $("#Result").text("Modifiers did not load");
        }
    });

這是我的Web服務的摘要:

...

[WebService(Namespace = "http://mynamespace.com/ModifierCodesService/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class ModifierCodesService : System.Web.Services.WebService
{

     /// <summary>
    /// Get a list of Modifiers
    /// </summary>
    /// <returns></returns>
    [WebMethod(EnableSession = true)]
    public Modifier[] GetModifierList()
    {
        return GetModifiers();
    }
       /// <summary>
        /// Modifiers list from database
        /// </summary>
        /// <returns></returns>
        private Modifier[] GetModifiers()
        {
            List<Modifier> modifier = new List<Modifier>();
            ModifierCollection matchingModifiers = ModifierList.GetModifierList();
            foreach (Modifier ModifierRow in matchingModifiers)
            {
                modifier.Add(new Modifier(ModifierRow.ModifierCode, ModifierRow.Description));
            }
            return modifier.ToArray();
        }
    }

...

目標代碼:

 public static class ModifierList
    {

        /// <summary>
        /// Returns the Modifier Collection.
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static ModifierCollection GetModifierList()
        {

今天,我一直在通過與.Net Web Service通話的iPhone應用程序來解決這個問題。

我發現,如果我將內容類型更改為application / jsonrequest,則可以順利通過,並且能夠處理Web服務器中的數據。

只是出於笑容,我在我的web.config中添加了上面提到的行,但是它沒有使application / json工作。

我對ASMX Web服務使用了JQuery AJAX JSON調用。 它在所有瀏覽器中均可完美運行。 我正在使用安裝了ASP.NET AJAX擴展(捆綁在3.5中)的.NET 2.0。

我班的裝飾師和你的一樣。 我的方法僅具有[WebMethod(EnableSession = true)]裝飾器。 但是,我的web.config在其httpHandlers部分中具有以下條目:

<add verb="*" path="*jsonservice.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

我的jquery調用如下所示:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "path/to/service/jsonservice.asmx/MethodName",
    data: JSON.stringify(DTO),
    dataType: "json",
    success: function(rtrn) { alert("good"); },
    error: function(req, msg, err) { alert("bad"); }
});

本文是我所學的基礎。

不知道這是否可能是如此簡單,但是我正在使用jQuery從我的Web方法中回調JSON。

我看到的主要區別是類的屬性

[System.Web.Script.Services.ScriptService]

    [WebService(Namespace = "http://MyNameSpace")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class Web : System.Web.Services.WebService{

       [WebMethod]
       public string TestMethod(){
         return "Testing";
       }

    }

我必須假設您正在使用3.5框架,因為這是公開JSON網絡方法的唯一方法。

我從jQuery發出的呼叫看起來幾乎完全相同,因此沒有問題。

如果要在IE中進行測試,請嘗試從contentType屬性中刪除字符集聲明(即,它應如下所示:

contentType: "application/json",

我還沒有找到原因,但是當使用“ charset=UTF-8 ”部分進行JSON調用時,IE似乎有點曲折。

替代文字

我認為您正在尋找WebInvoke或WebGet屬性,它使您可以指定Uri模板,正文樣式,請求和響應格式,例如:

[WebGet(ResponseFormat= WebMessageFormat.Json)]

鏈接可能會有所幫助。 WebInvoke也有類似的文章(主要用於發布)。

看起來您必須在scriptMethod標記中指定json作為響應格式。 這是來自vb.net,但我確定您知道:

ResponseFormat:= ResponseFormat.Json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM