问题发送JSON对象成功地为asp.net的WebMethod,使用jQuery [英] Problem sending JSON object succesfully to asp.net WebMethod, using jQuery

查看:144
本文介绍了问题发送JSON对象成功地为asp.net的WebMethod,使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这方面的工作3小时,并放弃了。 我只是想将数据发送到一个asp.net web方法,使用jQuery。 这些数据基本上是一帮键​​/值对。所以我试图创建一个数组,并添加了对到该阵列。

我的WebMethod(aspx.cs)看起来是这样的(这可能是错误的,我建立在JavaScript是什么,我只是不知道):

  [WebMethod的]
    公共静态字符串SaveRecord(名单<对象>的项目)
    .....
 

下面是我的示例的javascript:

  

VAR项目=新的阵列;

  VAR数据1 = {compId:1,formId:531};
    VAR数据2 = {compId:2,formId:77};
    变种DATA3 = {compId:3,formId:99};
    变种DATA4 = {状态:2,statusId:8};
    VAR数据5 = {名称:值,值:myvalue的};

    项目[0] = DATA1;
    项[1] = data2的;
    项[2] = DATA3;
    项[3] = DATA4;
    项[4] =数据5;
 

 这是我的jQuery ajax调用:

VAR的选择= {
        错误:函数(MSG){
            警报(msg.d);
        },
        键入:POST,
        网址:PackageList.aspx / SaveRecord
        数据:{项目:项目},
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        异步:假的,
        成功:函数(响应){
            VAR的结果= response.d;
        }
    };
    jQuery.ajax(选件);
 

我得到的错误 - 无效的JSON原始:项目 -

所以...如果我这样做:

  

VAR DTO = {'项目':项目};

和设置的数据参数是这样的:

  

数据:JSON.stringify(DTO)

然后我得到这个错误:

 无法将类型\ u0027System.String \ u0027对象输入\ u0027System.Collections.Generic.List`1 [System.Object的] \ u0027
 

我一直在兜圈子了几个小时。 有人请帮助我的小脑中的身影了这一点。

由于一吨。

解决方案

在使用AJAX.NET我总是让输入参数只是一个普通的旧对象,然后使用JavaScript解串器将其隐蔽到任何类型的我想要的。至少这样你可以在调试,看看是什么类型的对象的Web方法recieving。

您需要使用jQuery当你的对象转换为字符串

<%@页面语言=C#AutoEventWireup =真正的codeBehind =WebForm1.aspx.cs中继承=CustomEquip.WebForm1%>

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/ XHTML1-transitional.dtd>

< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <冠军>< /标题>
< /头>
<身体GT;
    <表格ID =Form1的=服务器>
        < ASP:ScriptManager的ID =SM=服务器的EnablePageMethods =真正的>
            <脚本>
                < ASP:的ScriptReference路径=〜/ JS /的jquery.js/>
            < /脚本>
        < / ASP:ScriptManager的>
        < D​​IV>< / DIV>
    < /形式GT;
< /身体GT;
< / HTML>
<脚本类型=文/ JavaScript的LANGUAGE =JavaScript的>
    变种项= [{compId:1,formId:531},
        {compId:2,formId:77},
        {compId:3,formId:99},
        {状态:2,statusId:8},
        {名称:值,值:myvalue的}];

        //使用Ajax.Net方法
        PageMethods.SubmitItems(项目,
            功能(响应){VAR的结果= response.d; },
            函数(MSG){警报(msg.d)},
            空值);

        //使用jQuery AJAX方法
        VAR的选择= {错误:函数(MSG){警报(msg.d); },
                        类型:POST,网址:WebForm1.aspx / SubmitItems
                        数据:{项目:items.toString()} //数组字符串修复它*
                        的contentType:应用/ JSON的;字符集= UTF-8,
                        数据类型:JSON,
                        异步:假的,
                        成功:函数(响应){VAR的结果= response.d; }};
        jQuery.ajax(选件);
< / SCRIPT>
 

而code

背后

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Services;
使用System.Web.Script.Serialization;
使用System.Web.Script.Services;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;

命名空间CustomEquip
{
    [ScriptService]
    公共部分类WebForm1的:System.Web.UI.Page
    {
        保护无效的Page_Load(对象发件人,EventArgs的)
        {

        }
        [WebMethod的]
        公共静态无效SubmitItems(对象项)
        {
            //这里突破点
            名单<对象> lstItems =新JavaScriptSerializer()ConvertToType<列表<对象>>(项目);
        }
    }
}
 

I've been working on this for 3 hours and have given up. i am simply trying to send data to an asp.net web method, using jQuery. The data is basically a bunch of key/value pairs. so i've tried to create an array and adding the pairs to that array.

My WebMethod(aspx.cs) looks like this (this may be wrong for what i'm building in javascript, i just dont know):

   [WebMethod]
    public static string SaveRecord(List<object> items)
    .....

Here is my sample javascript:

var items = new Array;

    var data1 = { compId: "1", formId: "531" };
    var data2 = { compId: "2", formId: "77" };
    var data3 = { compId: "3", formId: "99" };
    var data4 = { status: "2", statusId: "8" };
    var data5 = { name: "Value", value: "myValue" };

    items[0] = data1;
    items[1] = data2;
    items[2] = data3;
    items[3] = data4;
    items[4] = data5;

Here is my jQuery ajax call:

var options = {
        error: function(msg) {
            alert(msg.d);
        },
        type: "POST",
        url: "PackageList.aspx/SaveRecord",
        data: { 'items': items },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function(response) {
            var results = response.d;
        }
    };
    jQuery.ajax(options);

i get the error -Invalid JSON primitive: items.-

so...if i do this:

var DTO = { 'items': items };

and set the data parameter like this:

data: JSON.stringify(DTO)

then i get this error:

Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.List`1[System.Object]\u0027

I've been going in circles for hours. someone please help my small brain figure this out.

thanks a ton.

解决方案

When using AJAX.NET I always make the input parameter just a plain old object and then use the javascript deserializer to covert it to whatever type I want. At least that way you can debug and see what type of object the web method in is recieving.

You need to convert your object to a string when using jQuery

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CustomEquip.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true">
            <Scripts>
                <asp:ScriptReference Path="~/js/jquery.js" />
            </Scripts>
        </asp:ScriptManager>
        <div></div>
    </form>
</body>
</html>
<script type="text/javascript" language="javascript">
    var items = [{ compId: "1", formId: "531" },
        { compId: "2", formId: "77" },
        { compId: "3", formId: "99" },
        { status: "2", statusId: "8" },
        { name: "Value", value: "myValue"}];

        //Using Ajax.Net Method
        PageMethods.SubmitItems(items,
            function(response) { var results = response.d; },
            function(msg) { alert(msg.d) },
            null);

        //using jQuery ajax Method
        var options = { error: function(msg) { alert(msg.d); },
                        type: "POST", url: "WebForm1.aspx/SubmitItems",
                        data: {"items":items.toString()}, // array to string fixes it *
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        async: false, 
                        success: function(response) { var results = response.d; } }; 
        jQuery.ajax(options);
</script>

And the Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomEquip
{
    [ScriptService]
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static void SubmitItems(object items)
        {
            //break point here
            List<object> lstItems = new JavaScriptSerializer().ConvertToType<List<object>>(items);
        }
    }
}

这篇关于问题发送JSON对象成功地为asp.net的WebMethod,使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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