奇怪的json响应回jQuery

时间:2014-06-20 12:06:38

标签: c# jquery ajax post

不知何故,C#将这个奇怪的“d”属性名称添加到响应JSON中。我不知道如何解决这个问题。

电流:

  

{d:“{”message“:”HTMLhttp://www.google.si“,”success“:false}”}

应该是这样的:

  

{ “消息”: “HTMLhttp://www.google.si”, “成功”:假}

这个“d”从哪里来?

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

namespace WebApplication1
{
    [WebService(Namespace = "WebApplication1")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public String CreatePdf(String fromFormat, String targetUrl)
        {
            response obj = new response
            {
                message = fromFormat + targetUrl,
                success = false
            };

            return new JavaScriptSerializer().Serialize(obj);
        }
    }

    public class response
    {
        public string message = "";
        public Boolean success = false;
    }
}

jQuery的:

    $.ajax({
        url: 'http://localhost:56536/WebService1.asmx/CreatePdf?fromFormat=ONEJ',
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        data: JSON.stringify({
            fromFormat: 'HTML',
            targetUrl: 'http://www.google.si'
        }),
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data.responseText);
        },
        async: false,
        dataType: "json",
    });

0 个答案:

没有答案