Jquery Ajax 将 JSON 发布到 Web 服务 [英] Jquery Ajax Posting JSON to webservice

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

问题描述

我正在尝试将 JSON 对象发布到 asp.net 网络服务.

I am trying 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 对我的 JSON 对象进行字符串化.

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

我正在使用 jquery 将其发布到我的网络服务.

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);
        }
  });

我收到以下错误:

无效的 JSON 原语

Invalid JSON primitive

我发现了一堆与此相关的帖子,这似乎是一个非常普遍的问题,但我没有尝试解决这个问题.

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

我正在调用的网络服务函数是:

My webservice function that is being called is:

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

推荐答案

您提到使用 json2.js 对您的数据进行字符串化,但是 POST 的数据似乎是 URLEncoded JSON 您可能已经看过了,但是 这篇关于无效 JSON 原语的帖子 介绍了 JSON 被 URLEncoded 的原因.

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 将自动对请求的 POST 数据进行 JSON 反序列化,因此,如果您手动序列化 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);},
    error: function(errMsg) {
        alert(errMsg);
    }
});

避免无效 JSON 原语问题的关键是向 jQuery 传递 data 参数的 JSON 字符串,而不是 JavaScript 对象,以便 jQuery 不会尝试对您的数据进行 URLEncode.

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.";
}

如果您愿意,也可以接受一个数组,例如 Marker[] Markers.ASMX ScriptServices 使用的反序列化器 (JavaScriptSerializer) 非常灵活,并且会尽其所能将您的输入数据转换为您指定的服务器端类型.

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 Ajax 将 JSON 发布到 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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