I'm using jQuery to call an asmx and return some data. I'm making the call like this

我正在使用jQuery调用asmx并返回一些数据。我这样打电话

function getRequestInfo(event) {
        var id = $('#<%= RequestDaysId.ClientID %>').val();
        var formattedId = "{'id': '115'}";
        $.ajax({
            type: "Post",
            url: "services/VacationServices.asmx/GetVacationInfo",
            data: "{'id': '" + id + "'}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            processdata: true,
            success: function(data) {
                $('#<%=Note.ClientID %>').val(data.Note);
                $('.pendingrequestinfo').show().fadeIn(2000);
            },
            error: function(result, errortype, exceptionobject) {
                $('.failureMessage').fadeIn(2000).fadeOut(2000);
            }
        })
    };

Everything seems to be working fine, I set a break point in my success function and inspect the data object and see this.

一切似乎工作正常,我在我的成功函数中设置了一个断点并检查数据对象并看到这一点。

"{"Note":"this is a note","dayInfo":[{"ShortDate":"3/4/2010","DayType":"Vacation","HalfDay":""},{"ShortDate":"3/5/2010","DayType":"Vacation","HalfDay":""}]}"

The problem comes when I try to get the values out of the JSON. If I do something like data.Note, I get undefined back.

当我尝试从JSON中获取值时,问题就出现了。如果我做像data.Note这样的事情,我会得到未定义的回复。

It's late, It's Saturday and I've been at it all day, I sure would like a push in the right direction when it comes to parsing though my JSON.

已经很晚了,星期六,我整天都在这里,我确实希望通过解析我的JSON来推动正确的方向。

EDIT: I'm using Asp.net and JavaScriptSerializer.Serialize() to create the JSON. When I set a break point and inspect the 'data' object it looks to have a property d that contains the string that should be JSON.

编辑:我正在使用Asp.net和JavaScriptSerializer.Serialize()来创建JSON。当我设置一个断点并检查'data'对象时,它看起来有一个包含应该是JSON的字符串的属性d。

ANOTHER EDIT: If I do something like this in my success

另一个编辑:如果我在成功的过程中做了类似的事情

$('#<%=Note.ClientID %>').val(data.d.[0]);

I get the { opening curly brace. I guess i'm getting a string instead of JSON, but it seems to go against what the jquery api states about the return value when the datatype is set to JSON.

我得到{开口大括号。我想我得到的是一个字符串而不是JSON,但是当数据类型设置为JSON时,它似乎违背了jquery api关于返回值的内容。

Thanks guys. Jim

多谢你们。吉姆

4 个解决方案

#1


2

First make sure that the JSON string exists in the "d" variable in the response returned in the success callback. Next, in order to get the JSON object you will need to convert the string into the JSON. You can use the eval function or the JQuery built in function to convert string to JSON. I like the jquery-json plug in to convert string into JSON representation.

首先确保JSON字符串存在于成功回调中返回的响应中的“d”变量中。接下来,为了获取JSON对象,您需要将字符串转换为JSON。您可以使用eval函数或JQuery内置函数将字符串转换为JSON。我喜欢jquery-json插件将字符串转换为JSON表示。

Your code will look something like this:

您的代码将如下所示:

var jsonObject = eval('(' + data.d + ')'); 

Now, you can use jsonObject.Note or any other property.

现在,您可以使用jsonObject.Note或任何其他属性。

With jquery-json plugin you can do the following:

使用jquery-json插件,您可以执行以下操作:

var note = $.evalJSON(data.d).Note;

更多相关文章

  1. 加载函数加载页面但不能对数据执行任何操作
  2. 绑定和解除相同javascript函数的目的是什么?
  3. 如何设置请求标头字符串[重复]
  4. Jquery中的队列函数quene()、dequene()、clearQuene()
  5. Ajax_04之jQuery中封装的Ajax函数
  6. Jquery验证插件,获取错误字符串
  7. 怎么用js或jquery把一个函数b绑定到另一个函数a之后执行
  8. jQuery: 刨根问底 attr and prop两个函数的区别
  9. jQuery ajax问题 - 无法让我的函数工作

随机推荐

  1. Android Linux 内核介绍 (转)
  2. Android(安卓)项目实战视频资料 学习充电
  3. android网络编程——使用Android中的网络
  4. Android Studio:AndroidX的迁移
  5. Android 中自定义View(三)
  6. android app模拟 persistent 属性可以保
  7. android ndk 开发之 在 应用程序中使用 j
  8. [置顶] android调用第三方库——第二篇—
  9. Relativelayout的一些属性
  10. Android摄像头照相机技术-android学习之