Home  >  Article  >  Web Front-end  >  The javascript function call parameters and the background c# are not in the same file

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

王林
王林Original
2023-05-21 11:40:38386browse

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

}
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 IEnumerablec4d63d0a5af8914d4c3b80dc00e28749 Get([FromQuery] DateTime startDate, [FromQuery] int daysCount)
{

//code here

}

2. Pass parameters through HTTP POST:

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

//code here

}

3. Pass parameters through the query string:

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

//code here

}

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);
    }
});

}

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!

Statement:
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
Previous article:How to center cssNext article:How to center css