用JSON响应失意 [英] Frustrated with Json Response

查看:126
本文介绍了用JSON响应失意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,这里是我的问题。我使用jQuery的$就以一系列的值传递回一个Web方法。 Web方法所采用的值,创建一个对象,然后将其发送回作为JSON来调用页面。一旦我得到的回应回到我无法访问响应并显示它的值。

任何人能解释什么,我需要做的,使这项工作?

jQuery的脚本:

  $(文件)。就绪(函数(){
    $(#创建)。点击(函数(){
        变种名称= $('#姓名)VAL()。
        VAR公司= $('#公司)VAL()。
        。VAR位置= $('#位置)VAL();
        VAR PHONENUMBER = $('#PHONENUMBER)VAL()。
        。VAR国家= $('#国)VAL();        $阿贾克斯({
            键入:POST,
            网址:WebService.asmx / MakeEmployee
            数据:{名称:'+姓名+
                          ',公司本着:'+公司+
                          ',位置:+位置+
                          ',电话号码:'+ PHONENUMBER +
                          '国家:'+国家+
                          '},
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON
            成功:函数(MSG){
                AjaxSucceeded(msg.d);
            }
        });
    });    功能AjaxSucceeded(数据){
        // VAR项目= jQuery.parseJSON(数据)//这不会为我工作。
        $(#反应)。HTML(
            < UL><立GT;+ data.Name +
            < /李><立GT;+ data.Company +
            < /李><立GT;+ data.Address +
            < /李><立GT;+ data.Phone +
            < /李><立GT;+ data.Country +
            &所述; / UL>中
            );
        };
  });

Web方法:

  [的WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共字符串MakeEmployee(字符串名称,字符串的公司,
              字符串位置,字符串phoneNumber的,串国家)
{
    员工E =新员工(姓名,公司,地点,phoneNumber的,国家);
    返回新的JavaScriptSerializer()序列化(E)。
}

和响应说我找回:

  {D:\\名称\\:\\鲍勃\\,
          \\公司\\:\\谷歌\\,
          \\地址\\:\\家\\,
          \\电话\\:\\123 \\,
          \\国家\\:\\美国\\}

这是什么,我认为我应该得到回:

  {名:鲍勃,
     公司:谷歌,
      地址:家,
      手机:123,
      国:USA}

我得到一次页面错误再次渲染是这样的:

 •未定义
•未定义
•未定义
•未定义
•未定义


解决方案

您回应就已经被解析为JSON,因此它已经是一个对象......没有必要再分析它只是用它直接,就像这样:

 函数AjaxSucceeded(数据){
    $(#反应)。HTML(
        < UL><立GT;+ data.Name +
        < /李><立GT;+ data.Company +
        < /李><立GT;+ data.Address +
        < /李><立GT;+ data.Phone +
        < /李><立GT;+ data.Country +
        &所述; / UL>中
    );
}

{D:...} 包装是由ASP.Net增加,这是正常的行为。之后,您的问题没有正确的返回元素,你需要返回的对象的不是的字符串从ASP.Net,$ P $ 的pferably这样的:

  [的WebMethod,ScriptMethod(ResponseFormat = ResponseFormat.Json)
公务员MakeEmployee(字符串名称,字符串的公司,
  字符串位置,字符串phoneNumber的,串国家){
    返回新员工(姓名,公司,地点,phoneNumber的,国家);
}

...其中员工有你想要的JavaScript端的属性。让ASP.Net在这里处理序列,而不是直接做它,你会得到一个更清洁响应的整体。

So here is my problem. I'm using Jquery's $.ajax to pass back a series of values to a web method. The web method takes the values, creates an object and then sends it back as json to the calling page. Once I get the response back I am unable to access the response and display it's values.

Can anyone explain what I need to do to make this work?

The jquery script:

$(document).ready(function() {
    $("#create").click(function() {
        var name = $('#name').val();
        var company = $('#company').val();
        var location = $('#location').val();
        var phonenumber = $('#phonenumber').val();
        var country = $('#country').val();

        $.ajax({
            type: "POST",
            url: "WebService.asmx/MakeEmployee",
            data: "{name:'" + name +
                          "',company:'" + company +
                          "',location:'" + location +
                          "',phonenumber:'" + phonenumber +
                          "',country:'" + country +
                          "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                AjaxSucceeded(msg.d);
            }
        });
    });

    function AjaxSucceeded(data) {
        //var item = jQuery.parseJSON(data) // this doesn't work for me.
        $("#response").html(
            "<ul><li> " + data.Name +
            "</li><li> " + data.Company +
            "</li><li> " + data.Address +
            "</li><li> " + data.Phone +
            "</li><li> " + data.Country +
            "</ul> "
            );
        };
  });

The web method:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string MakeEmployee(string name, string company, 
              string location, string phoneNumber, string country)
{
    Employee e = new Employee(name, company, location, phoneNumber, country);
    return new JavaScriptSerializer().Serialize(e);
}

And the response that I'm getting back:

{"d":"\"Name\":\"bob\",
          \"Company\":\"google\",
          \"Address\":\"home\",
          \"Phone\":\"123\",
          \"Country\":\"usa\"}"}

This is what I think I should be getting back:

{"Name":"bob",
     "Company":"google",
      "Address":"home",
      "Phone":"123",
      "Country":"usa"}

The error I get once the pages renders again is this:

•undefined
•undefined
•undefined
•undefined
•undefined

解决方案

Your response will already be parsed as JSON, so it's already an object...no need to parse it again just use it directly, like this:

function AjaxSucceeded(data) {
    $("#response").html(
        "<ul><li> " + data.Name +
        "</li><li> " + data.Company +
        "</li><li> " + data.Address +
        "</li><li> " + data.Phone +
        "</li><li> " + data.Country +
        "</ul> "
    );
}

The { d: ... } wrapper is added by ASP.Net, that's normal behavior. After that your issue is the element not returned correctly, you need to return an object not a string from ASP.Net, preferably this:

[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Employee MakeEmployee(string name, string company, 
  string location, string phoneNumber, string country) {
    return new Employee(name, company, location, phoneNumber, country);
}

...where Employee has the properties you want on the JavaScript side. Let ASP.Net handle the serialization here instead of doing it directly, you'll get a cleaner response overall.

这篇关于用JSON响应失意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆