简体   繁体   English

ASP.NET - 将值从 JavaScript 发送到 Sql 服务器数据库

[英]ASP.NET - Send value from JavaScript to Sql Server Database

I am doing a small application with asp.net.我正在用 asp.net 做一个小应用程序。 I have a JavaScript file that gives me a value which I want to store inside an SQL Server Database but I have no clue how.我有一个 JavaScript 文件,它给了我一个我想存储在 SQL 服务器数据库中的值,但我不知道如何。 The values I want to add are response in the code below:我要添加的值是下面代码中的响应:

function statusChangeCallback(response) {
    console.log(response);
    if (response.status === 'connected') {
        console.log('Logged in and Authenticated');
        setELements(true);
        testAPI();
    } else {
        console.log('Not authenticated');
    setELements(false);
    }
}

Your question has fundamental problems.你的问题有基本问题。 Firstly, you can not access any DB directly from the Front-end of your application.首先,您不能直接从应用程序的前端访问任何数据库。 Your changes to the database are made with your back-end, which here is.Net.您对数据库的更改是使用您的后端进行的,这里是.Net。 If you are using MVC (Model,View,Controller) you can create needed controller and models, then you can make db changes with context of your database.如果您使用 MVC(模型、视图、控制器),您可以创建所需的 controller 和模型,然后您可以使用数据库的上下文进行 db 更改。

You have to create a Action Method in your asp.net MVC Controller.cs File and call through it by using ajax call.您必须在 asp.net MVC Controller.cs 文件中创建一个操作方法,并使用 ajax 调用来调用它。

ajax call: ajax 致电:

jQuery.ajax({
url: 'YourController/Save',
type: "POST",
data: "{'Value' : " + str + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
             alert("a");
          },
failure: function (msg) { alert("Sorry!!! "); }
});

Action Method:行动方法:

public JsonResult Save(string value)

{

return Json("Saved");
}

Write your saving code in MVC Action method.在 MVC Action 方法中编写您的保存代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM