The javascript function call parameters and the background c# are not in the same file

王林
Release: 2023-05-21 11:40:38
Original
421 people have browsed it

JavaScript function call parameters and background C

#In the process of front-end development, JavaScript function calling is an essential step, but in some cases, the parameters required by the JavaScript function are different from what the background C# code can The provided parameters are not in the same file. At this point we need to find a way to handle this situation.

1. JavaScript function call parameters

In JavaScript, parameters need to be passed when calling a function, which can be passed in the following ways:

1. Pass string or numeric value :

function functionName(param1, param2){

//code here
Copy after login
Copy after login
Copy after login
Copy after login

}
functionName('hello', 1);

2. Pass the array:

function functionName(arr){
//code here
}
var myArray = ['apple', 'banana', 'orange'];
functionName(myArray);

3. Pass the object:

function functionName(obj){
//code here
}
var myObject = {name: 'Tom', age: 18};
functionName(myObject);

2. The background C# code provides parameters

In the background C# code, we usually complete the corresponding logical processing by calling the API interface. At this time, pass The way the parameters are used also needs attention. Generally speaking, there are several ways to pass parameters:

1. Pass parameters through HTTP GET:

[HttpGet]
public IEnumerable Get([FromQuery] DateTime startDate, [FromQuery] int daysCount)
{

//code here
Copy after login
Copy after login
Copy after login
Copy after login

}

2. Pass parameters through HTTP POST:

[HttpPost]
public IActionResult Post([FromBody] Person person)
{

//code here
Copy after login
Copy after login
Copy after login
Copy after login

}

3. Pass parameters through the query string:

public IActionResult GetPerson([FromQuery(Name = "id")] int personId)
{

//code here
Copy after login
Copy after login
Copy after login
Copy after login

}

3. How to solve the problem that the parameters are not in the same file

During the development process, it is very likely that There will be situations where the parameters are not in the same file. At this time we need to handle it accordingly. Here are some solutions:

1. Use AJAX asynchronous request:

Use AJAX in JavaScript functions The asynchronous request obtains the parameters in the background C# code, as follows:

function getParam(){

$.ajax({
    url: 'http://localhost:5000/api/person?id=1',
    type: 'GET',
    success: function(data){
        functionName(data);
    }
});
Copy after login

}

2. Store the parameters in the background C# code in Cookie :

Store the parameters that need to be passed in Cookie in the background C# code, and then obtain and process the corresponding parameters through document.cookie in the JavaScript function.

3. Use HTML5 webstorage:

Store the parameters that need to be passed in localStorage or sessionStorage in the background C# code, and then obtain the corresponding parameters through localStorage or sessionStorage in the JavaScript function .

Summary

In front-end development, the connection between JavaScript function calls and parameters provided by the background C# code is very important. Some techniques are needed to solve the problem of parameters not being in the same file. , to ensure the normal operation of the program. The three solutions provided above can all be used to deal with this situation. Which one to use needs to be judged according to the specific needs of the project.

The above is the detailed content of The javascript function call parameters and the background c# are not in the same file. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template