使用C# ASP.NET Core 6 MVC上的jQuery进行AJAX。

huangapple go评论118阅读模式
英文:

AJAX WITH JQUERY ON C# ASP.NET Core 6 MVC

问题

这是代码的翻译部分:

<script>
	$(function(){
		var url =  '/Propiedades/Verificar'; 
		var correo = $('#correo')

		var data = {correoPropietario: correo}

		$.post(url, data, function(data){})
	})
</script>
英文:

I'm loading the information from a SQL database and I have a stored procedure, so I need a method to load.

This is my controller

[HttpPost]
public IActionResult Verificar(string correoPropietario)
{
	var verificarCorreo = _PropiedadesData.Verificar(correoPropietario);

	return Json(verificarCorreo);
}

How can I get back from the AJAX?

This is my AJAX call:

<script>
	$(function(){
		var url =  '/Propiedades/Verificar'; 
		var correo = $('#correo')

		var data = {correoPropietario: correo}

		$.post(url,data).donde(function(data){})
	})
</script>

答案1

得分: 0

您可以向您的控制器发送请求,然后可以读取控制器返回的响应。

我已经检查了您的Ajax代码片段,并发现了一个问题,您在其中使用了$.post(url,data).donde(function(data){}),这是不正确的。

在Ajax中,没有名为donde的东西,我认为这是一个拼写错误。正确的方式应该如下:

Ajax请求格式:

HTML:

<strong><span id="appendHere"></span></strong>
<hr />
<button type="button" name="submitButton" value="Edit" class="btn btn-primary form-control"
        onclick="GetAjaxResponse()">
    提交请求
</button>

Javascript:

@section scripts {
    <script>
        function GetAjaxResponse() {
            var url = 'http://localhost:5094/YourController Name/Verificar';
            var correo = "Kiron";
            $.post(url, { correoPropietario: correo }, function (result) {
                alert("响应");
                console.log(result);
                $("#appendHere").html(result);
            });
        }
    </script>
}

注意: 这里需要记住的一点是,您需要读取function(result) ,在这里您将获得控制器的响应数据。

输出:

使用C# ASP.NET Core 6 MVC上的jQuery进行AJAX。

注意: 如果您想了解更多详细信息,可以在此处查看更多示例

英文:

> how can I get back from the AJAX, this is my AJAX

You could send request to your contrroller and then can read the response what your controller returns.

I have checked your ajax code snippet and found a issue where you have used $.post(url,data).donde(function(data){}) which is incorrect.

In ajax nothing as donde which I am assuming a typos. The correct way you could refer as following:

Ajax Request Format:

HTML:

&lt;strong&gt;&lt;span id=&quot;appendHere&quot;&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/&gt;
&lt;hr /&gt;
&lt;button type=&quot;button&quot; name=&quot;submitButton&quot; value=&quot;Edit&quot; class=&quot;btn btn-primary form-control&quot;
        onclick=&quot;GetAjaxResponse()&quot;&gt;
    Submit Request
&lt;/button&gt;

Javascript:

@section scripts {
    &lt;script&gt;
        function GetAjaxResponse() {
            var url = &#39;http://localhost:5094/YourController Name/Verificar&#39;;
            var correo = &quot;Kiron&quot;;
            $.post(url, { correoPropietario: correo }, function (result) {
                alert(&quot;Response&quot;);
                console.log(result);
                $(&quot;#appendHere&quot;).html(result);
            });
        }
    &lt;/script&gt;
}

Note: Point you should remember here, you would need to read function(result) where you will get your controller response data.

Output:

使用C# ASP.NET Core 6 MVC上的jQuery进行AJAX。

Note: If you would like to know more details on it you could check more sample here.

huangapple
  • 本文由 发表于 2023年3月1日 08:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定