我正在开发一个asp.net mvc5项目。我使用webapi从另一台服务器获取数据,所以我使用下面的代码从服务器读取数据
public async System.Threading.Tasks.Task<string> webapicheap(string from,string to)
{
string url = "http://charter724.ir/webservice/list_period_date.php?from="+from+"&to="+to+"&id=367&%20key=EmlyCD8JOWX22cL8IuNBju5K5";
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// TempData["str"] = responseBody;
return responseBody;
}
所以我使用下面的脚本来调用上面的动作,所以在那之后一个名为responseBody的字符串被返回到视图中的ajax脚本,所以现在我想使用这个我不知道如何做到这一点的信息。请帮我做这个动作。
<script>
function f3() {
var from = $("#cou option:selected").val();
var to = $("#destin option:selected").val();
$.ajax({
url: '/Home/webapicheap',
data: { from: from,to:to },
type: 'post',
dataType: 'text',
success: function (data) {
// alert(data);
results = JSON.parse(data);
// alert(data.data)
//if (country_id != "" && zone_code != "" && duration != "" && birthday != "") {
// fPrice(data.token);
//}
$("#destin").html;
$.each(results, function (idx, res) {
alert(res.result.price)
$("#lbl1").append("<div>" + res.result.price + "</div>");
})
},
error: function (x, y, z) {
alert('fail')
//alert(x.responseText);
//alert(z);
}
});
}
</script>
webapi的输出
{"from":"\u062a\u0647\u0631\u0627\u0646","from_code":"87","to":"\u0645\u0634\u0647\u062f","to_code":"109","result":[{"week":"\u0633\u0647 \u0634\u0646\u0628\u0647","date_flight":"05\/12","price":"","link":"Result.html?t=YTo2OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDIiO3M6NDoiZnJvbSI7aToxMDAwMDtzOjI6InRvIjtpOjEwMDAxO3M6Mzoic2VjIjtpOjE0NzAxNjAxNDE7czo2OiJzZWNrZXkiO2k6MTgzMztzOjQ6InR5cGUiO3M6Mzoic3lzIjt9"},{"price":"136,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/13","type_flight":"5","week":"\u0686\u0647\u0627\u0631\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDMiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"129,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/14","type_flight":"5","week":"\u067e\u0646\u062c\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDQiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"120,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/15","type_flight":"5","week":"\u062c\u0645\u0639\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDUiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"125,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/16","type_flight":"5","week":"\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDYiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"130,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/17","type_flight":"5","week":"\u06cc\u06a9\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDciO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"141,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/18","type_flight":"5","week":"\u062f\u0648\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDgiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"159,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/19","type_flight":"5","week":"\u0633\u0647 \u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMDkiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"181,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/20","type_flight":"5","week":"\u0686\u0647\u0627\u0631\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTAiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"183,400
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/21","type_flight":"5","week":"\u067e\u0646\u062c\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTEiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"171,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/22","type_flight":"5","week":"\u062c\u0645\u0639\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTIiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"171,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/23","type_flight":"5","week":"\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTMiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"141,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/24","type_flight":"5","week":"\u06cc\u06a9\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTQiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"148,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/25","type_flight":"5","week":"\u062f\u0648\u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTUiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="},{"price":"161,000
\u062a\u0648\u0645\u0627\u0646","from":"10000","to":"10001","date_flight":"05\/26","type_flight":"5","week":"\u0633\u0647 \u0634\u0646\u0628\u0647","link":"Result.html?t=YTo1OntzOjQ6ImRhdGUiO3M6MTA6IjIwMTYtMDgtMTYiO3M6NDoiZnJvbSI7czo1OiIxMDAwMCI7czoyOiJ0byI7czo1OiIxMDAwMSI7czozOiJzZWMiO2k6MTQ3MDE2MDE0MTtzOjY6InNlY2tleSI7aToxODMzO30="}],"ndate":1,"ldate":-1,"next_name":"15 \u0631\u0648\u0632 \u0628\u0639\u062f","prev_name":"15 \u0631\u0648\u0632 \u0642\u0628\u0644","path_name":"\u0645\u0633\u06cc\u0631 \u067e\u0631\u0648\u0627\u0632\u06cc "}
答案 0 :(得分:0)
您的JSON结果包含result
属性下的实际结果数组。
尝试将行results = JSON.parse(data);
更改为results = JSON.parse(data).result;
此外,行$("#destin").html;
无效 - 删除它。