AJAX post data to DB without ASP:button












0















I retrieve a value in js and i want to insert it in the DB with ajax post. Is it possible to do that without asp:button to bind the value to the code behind?



UPDATE here is the Js:



function readXML() {
var xml = new XMLHttpRequest();
xml.open('GET', 'toXml/file.xml', false);
xml.send();
var xmlData = xml.responseXML;
if (!xmlData) {
xmlData = (new DOMParser()).parseFromString(xml.responseText, 'text/xml');
}

var itemsID = xmlData.getElementsByTagName("entry");
var itemsTitle = xmlData.getElementsByTagName("entry");
var itemsCustomer = xmlData.getElementsByTagName("d:Customer");
var itemsBrand = xmlData.getElementsByTagName("d:Brand");
var itemsCountries = xmlData.getElementsByTagName("d:Countries");
var itemsElement = xmlData.getElementsByTagName("d:element");

var i, k, j, ID, Title, Customer, Brand, Countries;

var list = ;

for (i = 0; i < itemsTitle.length; i++) {

var Brands = "";
var Country = "";

itemI = itemsID[i];
itemT = itemsTitle[i];
itemCr = itemsCustomer[i];
itemB = itemsBrand[i];
itemCs = itemsCountries[i];
itemEl = itemsElement[k];

ID = itemI.getElementsByTagName("d:Id")[0].firstChild.data;

Title = itemT.getElementsByTagName("d:Title")[0].firstChild.data;

Customer = itemCr.getElementsByTagName("d:element")[0].firstChild.data;

for (k = 0; k < itemB.children.length; k++) {
Brand = itemB.getElementsByTagName("d:element")[k].firstChild.data;

if (k > 1) {
Brands += Brand + ",";
}
else {
Brands = Brand;
}
}

for (j = 0; j < itemCs.children.length; j++) {
Countries = itemCs.getElementsByTagName("d:element")[j].firstChild.data;

if (j > 1) {
Country += Countries + ",";
}
else {
Country = Countries;
}

}
list[i] = [ID, Title, Customer, Brands, Country];
}

var table = $('#table_id').DataTable({
lengthMenu: [[10, 25, 50, 200, -1], [10, 25, 50, 200, "All"]],
data: list,
columns: [
{ title: "ID" },
{ title: "Title" },
{ title: "Customer" },
{ title: "Brands" },
{ title: "Country" },
{ title: "Check", "defaultContent": "<button class="checkBtn" type="button">Check</button>" },
{ title: "Create", "defaultContent": "<button class="createBtn" type="button">Create</button>" }
],
columnDefs: [{
"targets": [0],
"visible": true,
"searchable": true
}]
});

$('#table_id').on('click', '.checkBtn', function () {
var data = table.row($(this).parents('tr')).data();
alert("ID:" + data[0] + "," + "Title:" + data[1] + "," + "Customer:" + data[2] + "," + "Brand:" + data[3] + "," + "Country:" + data[4]);
$.ajax({
type: "POST",
url: "Selida.aspx/InsertData",
data: JSON.stringify(data[0]),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {

alert("all good");
},
error: function (errMessage) {
alert("Error:" + errMessage);
}
});
});
}


Through C# im getting data and i save it into an xml file. Then with the js i retrieve that data and with Jquery datatables im displaying on the page.



I Have a method in c# called InsertData which is supposed to insert the data in the DB. I think that the problem is in the ajax call.










share|improve this question

























  • What have you tried so far? Can we see a code snippet?

    – Tsahi Asher
    Nov 15 '18 at 15:52











  • I updated my question, if you need more info tell me

    – ADeirme
    Nov 15 '18 at 16:04
















0















I retrieve a value in js and i want to insert it in the DB with ajax post. Is it possible to do that without asp:button to bind the value to the code behind?



UPDATE here is the Js:



function readXML() {
var xml = new XMLHttpRequest();
xml.open('GET', 'toXml/file.xml', false);
xml.send();
var xmlData = xml.responseXML;
if (!xmlData) {
xmlData = (new DOMParser()).parseFromString(xml.responseText, 'text/xml');
}

var itemsID = xmlData.getElementsByTagName("entry");
var itemsTitle = xmlData.getElementsByTagName("entry");
var itemsCustomer = xmlData.getElementsByTagName("d:Customer");
var itemsBrand = xmlData.getElementsByTagName("d:Brand");
var itemsCountries = xmlData.getElementsByTagName("d:Countries");
var itemsElement = xmlData.getElementsByTagName("d:element");

var i, k, j, ID, Title, Customer, Brand, Countries;

var list = ;

for (i = 0; i < itemsTitle.length; i++) {

var Brands = "";
var Country = "";

itemI = itemsID[i];
itemT = itemsTitle[i];
itemCr = itemsCustomer[i];
itemB = itemsBrand[i];
itemCs = itemsCountries[i];
itemEl = itemsElement[k];

ID = itemI.getElementsByTagName("d:Id")[0].firstChild.data;

Title = itemT.getElementsByTagName("d:Title")[0].firstChild.data;

Customer = itemCr.getElementsByTagName("d:element")[0].firstChild.data;

for (k = 0; k < itemB.children.length; k++) {
Brand = itemB.getElementsByTagName("d:element")[k].firstChild.data;

if (k > 1) {
Brands += Brand + ",";
}
else {
Brands = Brand;
}
}

for (j = 0; j < itemCs.children.length; j++) {
Countries = itemCs.getElementsByTagName("d:element")[j].firstChild.data;

if (j > 1) {
Country += Countries + ",";
}
else {
Country = Countries;
}

}
list[i] = [ID, Title, Customer, Brands, Country];
}

var table = $('#table_id').DataTable({
lengthMenu: [[10, 25, 50, 200, -1], [10, 25, 50, 200, "All"]],
data: list,
columns: [
{ title: "ID" },
{ title: "Title" },
{ title: "Customer" },
{ title: "Brands" },
{ title: "Country" },
{ title: "Check", "defaultContent": "<button class="checkBtn" type="button">Check</button>" },
{ title: "Create", "defaultContent": "<button class="createBtn" type="button">Create</button>" }
],
columnDefs: [{
"targets": [0],
"visible": true,
"searchable": true
}]
});

$('#table_id').on('click', '.checkBtn', function () {
var data = table.row($(this).parents('tr')).data();
alert("ID:" + data[0] + "," + "Title:" + data[1] + "," + "Customer:" + data[2] + "," + "Brand:" + data[3] + "," + "Country:" + data[4]);
$.ajax({
type: "POST",
url: "Selida.aspx/InsertData",
data: JSON.stringify(data[0]),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {

alert("all good");
},
error: function (errMessage) {
alert("Error:" + errMessage);
}
});
});
}


Through C# im getting data and i save it into an xml file. Then with the js i retrieve that data and with Jquery datatables im displaying on the page.



I Have a method in c# called InsertData which is supposed to insert the data in the DB. I think that the problem is in the ajax call.










share|improve this question

























  • What have you tried so far? Can we see a code snippet?

    – Tsahi Asher
    Nov 15 '18 at 15:52











  • I updated my question, if you need more info tell me

    – ADeirme
    Nov 15 '18 at 16:04














0












0








0








I retrieve a value in js and i want to insert it in the DB with ajax post. Is it possible to do that without asp:button to bind the value to the code behind?



UPDATE here is the Js:



function readXML() {
var xml = new XMLHttpRequest();
xml.open('GET', 'toXml/file.xml', false);
xml.send();
var xmlData = xml.responseXML;
if (!xmlData) {
xmlData = (new DOMParser()).parseFromString(xml.responseText, 'text/xml');
}

var itemsID = xmlData.getElementsByTagName("entry");
var itemsTitle = xmlData.getElementsByTagName("entry");
var itemsCustomer = xmlData.getElementsByTagName("d:Customer");
var itemsBrand = xmlData.getElementsByTagName("d:Brand");
var itemsCountries = xmlData.getElementsByTagName("d:Countries");
var itemsElement = xmlData.getElementsByTagName("d:element");

var i, k, j, ID, Title, Customer, Brand, Countries;

var list = ;

for (i = 0; i < itemsTitle.length; i++) {

var Brands = "";
var Country = "";

itemI = itemsID[i];
itemT = itemsTitle[i];
itemCr = itemsCustomer[i];
itemB = itemsBrand[i];
itemCs = itemsCountries[i];
itemEl = itemsElement[k];

ID = itemI.getElementsByTagName("d:Id")[0].firstChild.data;

Title = itemT.getElementsByTagName("d:Title")[0].firstChild.data;

Customer = itemCr.getElementsByTagName("d:element")[0].firstChild.data;

for (k = 0; k < itemB.children.length; k++) {
Brand = itemB.getElementsByTagName("d:element")[k].firstChild.data;

if (k > 1) {
Brands += Brand + ",";
}
else {
Brands = Brand;
}
}

for (j = 0; j < itemCs.children.length; j++) {
Countries = itemCs.getElementsByTagName("d:element")[j].firstChild.data;

if (j > 1) {
Country += Countries + ",";
}
else {
Country = Countries;
}

}
list[i] = [ID, Title, Customer, Brands, Country];
}

var table = $('#table_id').DataTable({
lengthMenu: [[10, 25, 50, 200, -1], [10, 25, 50, 200, "All"]],
data: list,
columns: [
{ title: "ID" },
{ title: "Title" },
{ title: "Customer" },
{ title: "Brands" },
{ title: "Country" },
{ title: "Check", "defaultContent": "<button class="checkBtn" type="button">Check</button>" },
{ title: "Create", "defaultContent": "<button class="createBtn" type="button">Create</button>" }
],
columnDefs: [{
"targets": [0],
"visible": true,
"searchable": true
}]
});

$('#table_id').on('click', '.checkBtn', function () {
var data = table.row($(this).parents('tr')).data();
alert("ID:" + data[0] + "," + "Title:" + data[1] + "," + "Customer:" + data[2] + "," + "Brand:" + data[3] + "," + "Country:" + data[4]);
$.ajax({
type: "POST",
url: "Selida.aspx/InsertData",
data: JSON.stringify(data[0]),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {

alert("all good");
},
error: function (errMessage) {
alert("Error:" + errMessage);
}
});
});
}


Through C# im getting data and i save it into an xml file. Then with the js i retrieve that data and with Jquery datatables im displaying on the page.



I Have a method in c# called InsertData which is supposed to insert the data in the DB. I think that the problem is in the ajax call.










share|improve this question
















I retrieve a value in js and i want to insert it in the DB with ajax post. Is it possible to do that without asp:button to bind the value to the code behind?



UPDATE here is the Js:



function readXML() {
var xml = new XMLHttpRequest();
xml.open('GET', 'toXml/file.xml', false);
xml.send();
var xmlData = xml.responseXML;
if (!xmlData) {
xmlData = (new DOMParser()).parseFromString(xml.responseText, 'text/xml');
}

var itemsID = xmlData.getElementsByTagName("entry");
var itemsTitle = xmlData.getElementsByTagName("entry");
var itemsCustomer = xmlData.getElementsByTagName("d:Customer");
var itemsBrand = xmlData.getElementsByTagName("d:Brand");
var itemsCountries = xmlData.getElementsByTagName("d:Countries");
var itemsElement = xmlData.getElementsByTagName("d:element");

var i, k, j, ID, Title, Customer, Brand, Countries;

var list = ;

for (i = 0; i < itemsTitle.length; i++) {

var Brands = "";
var Country = "";

itemI = itemsID[i];
itemT = itemsTitle[i];
itemCr = itemsCustomer[i];
itemB = itemsBrand[i];
itemCs = itemsCountries[i];
itemEl = itemsElement[k];

ID = itemI.getElementsByTagName("d:Id")[0].firstChild.data;

Title = itemT.getElementsByTagName("d:Title")[0].firstChild.data;

Customer = itemCr.getElementsByTagName("d:element")[0].firstChild.data;

for (k = 0; k < itemB.children.length; k++) {
Brand = itemB.getElementsByTagName("d:element")[k].firstChild.data;

if (k > 1) {
Brands += Brand + ",";
}
else {
Brands = Brand;
}
}

for (j = 0; j < itemCs.children.length; j++) {
Countries = itemCs.getElementsByTagName("d:element")[j].firstChild.data;

if (j > 1) {
Country += Countries + ",";
}
else {
Country = Countries;
}

}
list[i] = [ID, Title, Customer, Brands, Country];
}

var table = $('#table_id').DataTable({
lengthMenu: [[10, 25, 50, 200, -1], [10, 25, 50, 200, "All"]],
data: list,
columns: [
{ title: "ID" },
{ title: "Title" },
{ title: "Customer" },
{ title: "Brands" },
{ title: "Country" },
{ title: "Check", "defaultContent": "<button class="checkBtn" type="button">Check</button>" },
{ title: "Create", "defaultContent": "<button class="createBtn" type="button">Create</button>" }
],
columnDefs: [{
"targets": [0],
"visible": true,
"searchable": true
}]
});

$('#table_id').on('click', '.checkBtn', function () {
var data = table.row($(this).parents('tr')).data();
alert("ID:" + data[0] + "," + "Title:" + data[1] + "," + "Customer:" + data[2] + "," + "Brand:" + data[3] + "," + "Country:" + data[4]);
$.ajax({
type: "POST",
url: "Selida.aspx/InsertData",
data: JSON.stringify(data[0]),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {

alert("all good");
},
error: function (errMessage) {
alert("Error:" + errMessage);
}
});
});
}


Through C# im getting data and i save it into an xml file. Then with the js i retrieve that data and with Jquery datatables im displaying on the page.



I Have a method in c# called InsertData which is supposed to insert the data in the DB. I think that the problem is in the ajax call.







javascript c# ajax asp.net-ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 9:56









wanttobeprofessional

1,00731323




1,00731323










asked Nov 15 '18 at 15:34









ADeirmeADeirme

12




12













  • What have you tried so far? Can we see a code snippet?

    – Tsahi Asher
    Nov 15 '18 at 15:52











  • I updated my question, if you need more info tell me

    – ADeirme
    Nov 15 '18 at 16:04



















  • What have you tried so far? Can we see a code snippet?

    – Tsahi Asher
    Nov 15 '18 at 15:52











  • I updated my question, if you need more info tell me

    – ADeirme
    Nov 15 '18 at 16:04

















What have you tried so far? Can we see a code snippet?

– Tsahi Asher
Nov 15 '18 at 15:52





What have you tried so far? Can we see a code snippet?

– Tsahi Asher
Nov 15 '18 at 15:52













I updated my question, if you need more info tell me

– ADeirme
Nov 15 '18 at 16:04





I updated my question, if you need more info tell me

– ADeirme
Nov 15 '18 at 16:04












1 Answer
1






active

oldest

votes


















0














The answer is yes, i can post a value from js with an ajax call that has no relation with the server side



$.ajax({
type: "POST",
url: "Selida.aspx/CheckData",
data: JSON.stringify({ trala : data[0] }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {

alert("It's already in the DB");
},
error: function (errMessage) {
console.log("Error:" + JSON.stringify(errMessage));
}

});


and the parameter in the c# method must have the name "trala" in order to retrieve the value from the post.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322837%2fajax-post-data-to-db-without-aspbutton%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The answer is yes, i can post a value from js with an ajax call that has no relation with the server side



    $.ajax({
    type: "POST",
    url: "Selida.aspx/CheckData",
    data: JSON.stringify({ trala : data[0] }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function () {

    alert("It's already in the DB");
    },
    error: function (errMessage) {
    console.log("Error:" + JSON.stringify(errMessage));
    }

    });


    and the parameter in the c# method must have the name "trala" in order to retrieve the value from the post.






    share|improve this answer




























      0














      The answer is yes, i can post a value from js with an ajax call that has no relation with the server side



      $.ajax({
      type: "POST",
      url: "Selida.aspx/CheckData",
      data: JSON.stringify({ trala : data[0] }),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function () {

      alert("It's already in the DB");
      },
      error: function (errMessage) {
      console.log("Error:" + JSON.stringify(errMessage));
      }

      });


      and the parameter in the c# method must have the name "trala" in order to retrieve the value from the post.






      share|improve this answer


























        0












        0








        0







        The answer is yes, i can post a value from js with an ajax call that has no relation with the server side



        $.ajax({
        type: "POST",
        url: "Selida.aspx/CheckData",
        data: JSON.stringify({ trala : data[0] }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () {

        alert("It's already in the DB");
        },
        error: function (errMessage) {
        console.log("Error:" + JSON.stringify(errMessage));
        }

        });


        and the parameter in the c# method must have the name "trala" in order to retrieve the value from the post.






        share|improve this answer













        The answer is yes, i can post a value from js with an ajax call that has no relation with the server side



        $.ajax({
        type: "POST",
        url: "Selida.aspx/CheckData",
        data: JSON.stringify({ trala : data[0] }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () {

        alert("It's already in the DB");
        },
        error: function (errMessage) {
        console.log("Error:" + JSON.stringify(errMessage));
        }

        });


        and the parameter in the c# method must have the name "trala" in order to retrieve the value from the post.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 9:00









        ADeirmeADeirme

        12




        12






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322837%2fajax-post-data-to-db-without-aspbutton%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents