• asp.net webservice返回json问题


    使用jQuery $.ajax方法请求webservice

    一、方法返回值为string,将json格式的字符串返回

    设置contentType为"application/json;charset=utf-8"(若不设置contentType,返回的默认是xml标记的字符串,不能按照正常解析json的方式来解析)

    前台调用返回数据:

    success:function(data){
        if(data.d){
            data=eval("("+data.d+");");//
        }
        //……其他处理代码
    }
    

    当你做一些项目时,一些前端框架,默认使用$.ajax请求的时候,是没有加上contentType的,这种情况下,你如果想继续使用asp.net的webservice,你只能对框架的源文件进行修改,或者添加一个扩展js,覆盖原先的方法,但前端框架有的会经常更新,更新之后会有一些变动,你再去修改源文件或者再修改扩展的js?这太让人不爽了,若是别人接手你的项目,改起来也麻烦。

    二、方法返回值为void,通过Response对象,输出字符串

    此方法不需要设置contentType,通过以下方式返回

    HttpContext.Current.Reponse.Write("{"xx":"xx"");
    HttpContext.Current.Response.End();
    

    这样在前台获取的时候,返回的json对象无须使用data.d来获取

  • 相关阅读:
    zoj1137 poj1466
    poj3041
    zoj1455
    hdu1160 FatMouse's Speed
    zoj2770
    hdu1469
    hdu3169
    Mapped exception to response: 500 (Internal Server Error)
    Mapped exception to response: 500 (Internal Server Error)
    object is not a function
  • 原文地址:https://www.cnblogs.com/qingshang/p/3334659.html
Copyright © 2020-2023  润新知