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

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

问题描述

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

我的 json 看起来像这样:

var 标记 = { 标记": [{位置":128.3657142857143",markerPosition":7";},{位置":235.1944023323615",markerPosition":19";},{位置":42.5978231292517",markerPosition":-3";}]};

我正在使用 json2.js 来串接我的 JSON 对象.

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

 $.ajax({类型:POST",网址:/webservices/PodcastService.asmx/CreateMarkers",数据:标记,内容类型:应用程序/json;字符集=utf-8",数据类型:json",成功:功能(数据){警报(数据);},失败:功能(errMsg){警报(错误消息);}});

我收到以下错误:

<块引用>

无效的 JSON 原语

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

当萤火虫发布到服务器时,它看起来像这样:

<块引用>

markers%5B0%5D%5Bposition%5D=128.3657142857143&markers%5B0%5D%5BmarkerPosition%5D=7&markers%5B1%5D%5Bposition%5D=235.1944023332857143&markers%5B0%5D%5BmarkerPosition%5D=7&markers%5B1%5D%5Bposition%5D=235.1944023332857143&markers%5B0%5D%5BmarkerPosition%5D=235.194402333285702333285%Bmarker%Dmarkers=19&markers%5B2%5D%5Bposition%5D=42.5978231292517&markers%5B2%5D%5BmarkerPosition%5D=-3

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

[WebMethod]公共字符串CreateMarkers(字符串markerArray){返回收到的标记";}

解决方案

您提到使用 json2.js 来字符串化您的数据,但 POSTed 数据似乎是 URLEncoded JSON 您可能已经看过它,但是 这篇关于无效 JSON 原语的博文 介绍了 JSON 被 URLEncoded 的原因.

我会建议反对 将原始的、手动序列化的 JSON 字符串传递到您的方法中.ASP.NET 将自动对请求的 POST 数据进行 JSON 反序列化,因此如果您手动序列化并将 JSON 字符串发送到 ASP.NET,您实际上最终必须对 JSON 序列化字符串进行 JSON 序列化.

我会建议更多类似的东西:

var 标记 = [{ 位置":128.3657142857143",markerPosition":7";},{位置":235.1944023323615",markerPosition":19";},{位置":42.5978231292517",markerPosition":-3";}];$.ajax({类型:POST",网址:/webservices/PodcastService.asmx/CreateMarkers",//key 需要匹配你方法的输入参数(区分大小写).数据:JSON.stringify({标记:标记}),内容类型:应用程序/json;字符集=utf-8",数据类型:json",成功:功能(数据){警报(数据);},错误:函数(errMsg){警报(错误消息);}});

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

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

公共类标记{公共小数位{获取;放;}公共 int 标记位置 { 获取;放;}}[网络方法]公共字符串 CreateMarkers(List Markers){返回收到"+ Markers.Count + "标记.";}

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

I am trying to post a JSON object to a asp.net webservice.

My json looks like this:

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

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

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:

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

My webservice function that is being called is:

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

解决方案

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.

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

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

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

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