jQuery的阿贾克斯发布JSON到web服务 [英] Jquery Ajax Posting json to webservice

查看:135
本文介绍了jQuery的阿贾克斯发布JSON到web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我tryng发布一个JSON对象到asp.net web服务。

Hi all I am tryng to post a JSON object to a asp.net webservice.

我的JSON是这样的:

My json looks like this:

var markers = { "markers": [
  { "position": "128.3657142857143", "markerPosition": "7" },
  { "position": "235.1944023323615", "markerPosition": "19" },
  { "position": "42.5978231292517", "markerPosition": "-3" }
]};

我现在用的是json2.js到stringyfy我的JSON对象。

I am using the json2.js to stringyfy my json object.

和我使用jQuery来发布到我的web服务。

and i am using jquery to post it to my webservice.

  $.ajax({
        type: "POST",
        url: "/webservices/PodcastService.asmx/CreateMarkers",
        data: markers,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data){alert(data);},
        failure: function(errMsg) {
            alert(errMsg);
        }
  });

我收到以下错误:

I am getting the following error:

无效的JSON原始:

我发现了一堆关于这个职位,这似乎是一个很常见的问题,但没有我尝试修复该问题。

I have found a bunch of posts relating to this and it seems to be a really common problem but nothing i try fixes the issue.

当萤火虫什么被发布到它看起来像这样的服务器:

When firebug what is being posted to the server it looks like this:

markers%5B0%5D%5Bposition%5D=128.3657142857143&markers%5B0%5D%5BmarkerPosition%5D=7&markers%5B1%5D%5Bposition%5D=235.1944023323615&markers%5B1%5D%5BmarkerPosition%5D=19&markers%5B2%5D%5Bposition%5D=42.5978231292517&markers%5B2%5D%5BmarkerPosition%5D=-3

markers%5B0%5D%5Bposition%5D=128.3657142857143&markers%5B0%5D%5BmarkerPosition%5D=7&markers%5B1%5D%5Bposition%5D=235.1944023323615&markers%5B1%5D%5BmarkerPosition%5D=19&markers%5B2%5D%5Bposition%5D=42.5978231292517&markers%5B2%5D%5BmarkerPosition%5D=-3

这是被称为我的web服务的功能是:

My webservice function that is being called is:

[WebMethod]
public string CreateMarkers(string markerArray)
{
    return "received markers";
}

任何帮助,您可以给将是非常美联社preciated,我知道有关于这一点,但真的没有什么我发现帮助。

Any help you can give would be really appreciated, I know there are alot of posts about this but really nothing i have found helped.

推荐答案

您提到了使用json2.js到字符串化数据,但发布的数据似乎URLEn codeD JSON你可能已经看到了,但< A HREF =htt​​p://encosia.com/asmx-scriptservice-mistake-invalid-json-primitive/相对=nofollow>这篇文章关于无效的JSON原始涵盖为什么JSON是被URLEn codeD。

You mentioned using json2.js to stringify your data, but the POSTed data appears to be URLEncoded JSON You may have already seen it, but this post about the invalid JSON primitive covers why the JSON is being URLEncoded.

提醒的传递一个原始的,手动序列化的JSON字符串到方法。 ASP.NET会自动反序列化的JSON请求的POST数据,所以如果你手动序列化和发送一个JSON字符串,ASP.NET,您实际上最终不得不JSON序列化的JSON序列化字符串。

I'd advise against passing a raw, manually-serialized JSON string into your method. ASP.NET is going to automatically JSON deserialize the request's POST data, so if you're manually serializing and sending a JSON string to ASP.NET, you'll actually end up having to JSON serialize your JSON serialized string.

我会建议沿着这些路线的更多的东西:

I'd suggest something more along these lines:

var markers = [{ "position": "128.3657142857143", "markerPosition": "7" },
               { "position": "235.1944023323615", "markerPosition": "19" },
               { "position": "42.5978231292517", "markerPosition": "-3" }];

$.ajax({
    type: "POST",
    url: "/webservices/PodcastService.asmx/CreateMarkers",
    // The key needs to match your method's input parameter (case-sensitive).
    data: JSON.stringify({ Markers: markers }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){alert(data);},
    failure: function(errMsg) {
        alert(errMsg);
    }
});

的关键是避免无效的JSON原始的问题是通过jQuery的一个JSON字符串数据参数,而不是一个JavaScript对象,这样的jQuery不会尝试URLEn code数据。

The key to avoiding the invalid JSON primitive issue is to pass jQuery a JSON string for the data parameter, not a JavaScript object, so that jQuery doesn't attempt to URLEncode your data.

在服务器端,匹配方法的输入参数,你传递数据的形状:

On the server-side, match your method's input parameters to the shape of the data you're passing in:

public class Marker
{
  public decimal position { get; set; }
  public int markerPosition { get; set; }
}

[WebMethod]
public string CreateMarkers(List<Marker> Markers)
{
  return "Received " + Markers.Count + " markers.";
}

您还可以接受一个数组,如标记[]标记,如果preFER。这ASMX ScriptServices使用解串器(JavaScriptSerializer)是pretty的灵活性,并会竭尽所能,为您的输入数据转换成您所指定的服务器端的类型。

You can also accept an array, like Marker[] Markers, if you prefer. The deserializer that ASMX ScriptServices uses (JavaScriptSerializer) is pretty flexible, and will do what it can to convert your input data into the server-side type you specify.

这篇关于jQuery的阿贾克斯发布JSON到web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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