gpt4 book ai didi

javascript - 调用返回 JSON 的操作方法时,Ajax 和 getJSON 之间有什么区别

转载 作者:行者123 更新时间:2023-11-28 02:22:45 25 4
gpt4 key购买 nike

我正在读一本关于 asp.net MVC 的书,我发现了调用返回 JSON: 的 Action 方法的不同方法,可以使用 Ajax 或 getJSOn,所以这两种方法相当于:-

$.ajax({
type: "GET",
url: "http://localhost:11279/test/testcall",
dataType: "json",
success: function (result) {
var message = result.Title + ": $" + result.CurrentPrice;
$('#Result').html(message);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + errorThrown);
}
});

getJSON 是:-

<script type="text/javascript">
$(function () {
$.getJSON("http://localhost:11279/test/testcall",
function (data) {
$.each(data, function (key, val) {
var str = val.Description;
$('<li/>', { html: str }).appendTo($('#auctions'));
});
});
});
</script>

第二个问题

如果我想从 Controller 类调用上述操作方法或外部 Web 服务而不是使用 javaScript,那么我应该使用哪些 c-sharp 方法?以及如何传递从 Controller 返回的 JSON类到 View 。BR

最佳答案

getJson-方法允许通过对页面进行ajax调用来获取json数据。该方法只允许通过get方法传递参数,不允许传递参数。

Ajax () - 此方法比我们看到的所有其他方法提供更多的控制。您可以通过检查参数列表来找出差异

  • 对数据发送和响应数据提供更多控制。
  • 允许处理调用期间发生的错误。
  • 如果调用ajax页面成功,则允许处理数据。

回答2

您可以使用jquery + Ajax()函数在您的html页面中使用它..

这是给您的文章:Steps to Call WCF Service using jQuery .

类似这样的

function WCFJSON() {
var userid = "1";
Type = "POST";
Url = "Service.svc/GetUser";
Data = '{"Id": "' + userid + '"}';
ContentType = "application/json; charset=utf-8";
DataType = "json"; varProcessData = true;
CallService();
}

//function to call WCF Service
function CallService() {
$.ajax({
type: Type, //GET or POST or PUT or DELETE verb
url: Url, // Location of the service
data: Data, //Data sent to server
contentType: ContentType, // content type sent to server
dataType: DataType, //Expected data format from server
processdata: ProcessData, //True or False
success: function(msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}

关于javascript - 调用返回 JSON 的操作方法时,Ajax 和 getJSON 之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15406200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com