使用ajax发布jquery数组值

时间:2018-06-05 12:48:19

标签: c# jquery ajax webforms

我有以下jquery数组,它从我的表中的两列中获取值并显示它们的值:

$(function () {
                 $('#myButton').on('click', function () {

                     var myCollection = [];

                     $('#MainContent_gvKarakteristike tbody').find('tr:gt(0)').each(function () {
                         var row = this;
                         var myObj = {

                             label: valuefromType($(row).find($(row).find('td:eq(1)').children())),
                             opis: valuefromType($(row).find($(row).find('td:eq(2)').children()))
                         };
                         myCollection[myCollection.length] = myObj;

                     });

                     console.log(myCollection)

                     function valuefromType(control) {
                         var type = $(control).prop('nodeName').toLowerCase();
                         switch (type) {
                             case "input":
                                 return $(control).val();
                                 break;
                             case "span":
                                 return $(control).text();
                                 break;
                             case "select":
                                 return $(control).val();
                                 break;


                         }
                     }

                     $.ajax({
                         type: "POST",
                         url: "FirstPage.aspx",
                         data: { obj: myCollection },
                         contentType: "application/json; charset=utf-8",
                         dataType: "json",

                         success: function (response) {
                             console.log(response);
                         },
                         error: function (response) {
                             console.log(response);
                         }
                     });
                 });


             });

第一列的名称是'label',第二列的名称是数组的'opis'结果:

myCollection
(6) […]
​
0: Object { label: "1", opis: "Test1" }
​
1: Object { label: "2", opis: "Test2" }
​
2: Object { label: "3", opis: "Test3" }
​
3: Object { label: "5", opis: "1" }
​
4: Object { label: "9", opis: "Test5" }
​
5: Object { label: "15", opis: "Test6" }
​
length: 6

点击此按钮即可获取值:

<button id="myButton"  type="button">Save</button>

问题是我添加的这个ajax代码无法正常工作,我在控制台中收到的消息:

abort: function abort()
​
always: function always()
​
catch: function catch()
​
done: function add()
​
fail: function add()
​
getAllResponseHeaders: function getAllResponseHeaders()
​
getResponseHeader: function getResponseHeader()
​
overrideMimeType: function overrideMimeType()
​
pipe: function pipe()
​
progress: function add()
​
promise: function promise()
​
readyState: 4
​
responseText: "\r\n\r\n<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head><link href=\"/Content/Site.css\" rel=\"stylesheet\"/>\r\n<link href=\"favicon.ico\" rel=\"shortcut icon\" type=\"image/x-icon\" /><meta name=\"viewport\" content=\"width=device-width\" /><title>\r\n\r\n</title></head>\r\n<body>\r\n    <form method=\"post\" action=\"./FirstPage.aspx\" id=\"ctl01\">\r\n<div class=\"aspNetHidden\">\r\n<input type=\"hidden\" name=\"__EVENTTARGET\" id=\"__EVENTTARGET\" value=\"\" />\r\n<input type=\"hidden\" name=\"__EVENTARGUMENT\" id=\"__EVENTARGUMENT\" value=\"\" />\r\n<input type=\"hidden\" name=\"__LASTFOCUS\" id=\"__LASTFOCUS\" value=\"\" />\r\n<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"88YX1NFIdndeRDD3+H+uL/sB8TWrYi/KKBNyERGGyVOayBK1cIe3xMNlRnLfuxJglI1Lx5lDzWZANq6Jb4h+4zyXEaYHueYaGk9GfVOAFDIHLoKCWA6b6sEafoGlGnd3FkVgTMxOL76OAp/6VwLbLEKgt+4bx+9PYOQ7RoWY3nVWsxssYBYn5rniBznrNsrgOYob3bDSMEd4MIB4hHsfheJ5rYAJHOqM7lDTvK+yeyLbgGctO61oF5giKUwhXWu40qtajMsDA1d3RAqJcaV7XefVEv2Eo527382EYXgbVHubU8wnAcogYgzFGqt53ot06jjGPvLn+6fMpJZoe5NR0mQ2WwKbukpZea62nOS8wG3fEJmgtBV…"
​
setRequestHeader: function setRequestHeader()
​
state: function state()
​
status: 200
​
statusCode: function statusCode()
​
statusText: "OK"
​
then: function then()
​
__proto__: Object { … }

我需要将数组值POST回到后端部分,C#可以将其发送到数据库。

有人可以帮助ajax部分吗? 我做错了什么?

提前致谢!

0 个答案:

没有答案