How to return Json from WCF Service?

前端 未结 2 1197
半阙折子戏
半阙折子戏 2020-12-19 03:14

I have the piece of code below of a template Ajax enabled WCF service. What can i do to make it return JSon instead of XML? thanks.

using System; 
using Sys         


        
相关标签:
2条回答
  • 2020-12-19 03:37

    Have you tried:

    [WebGet(ResponseFormat= WebMessageFormat.Json)]
    
    0 讨论(0)
  • 2020-12-19 03:50

    If you want to use the POST verb as in $.ajax({ type: "POST", ...) you will need to markup your method with [WebInvoke(Method="POST"].

    Since you marked it up with [WebGet] (which is equivalent to [WebInvoke(Method="GET")]) you should call the service using the GET verb, e.g.:

    $.ajax({ type: "GET", ...) or use $.get(url, data, ...) (see jQuery.get for more info).

    And you'll need to set the ResponseFormat to Json, as tomasr already pointed out.

    0 讨论(0)
提交回复
热议问题