我正在尝试从网站获取一些数据,首先尝试使用localhost。但Get请求失败。将其更改为POST工作正常。
Error: The Resource cannot be found.
控制器操作:
[HttpGet()]
public ActionResult GetInformation(string id)
{
Uri uri = new Uri("http://localhost:65076/ShowDetails?id=" + id);
HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
response.Close();
return Json(new { data = tmp });
}
JavaScript函数:
function getInformation() {
var link = '/Home/GetInformation';
var id = '11111';
$.ajax({
type: 'GET',
url: link,
data: { id: id },
dataType: 'json',
success: function (result) {
$.each(result.data, function (item, value) {
...
});
},
error: function (result) {
...
}
});
};
答案 0 :(得分:2)
由于这是GET,请在查询字符串中传递数据:
function getInformation() {
var link = '/Home/GetInformation?id=11111';
$.ajax({
type: 'GET',
url: link,
dataType: 'json',
success: function (result) {
$.each(result.data, function (item, value) {
...
});
},
error: function (result) {
...
}
});
};