Aus*_*tin 5 javascript c# ajax asp.net-mvc json
我想要的是在对跨域进行Ajax调用时保护我的开发人员密钥.在我直接进入网址并插入密钥之前.像这样
$.ajax({
url: "https://na.api.pvp.net/api/lol/na/v2.3/team/TEAM-ID?api_key=mykey",
type: "GET",
data: {},
success: function (json) {
console.log(json);
console.log(json[teamID].name);
console.log(json[teamID].fullId);
console.log(json[teamID].roster.ownerId);
console.log(json[teamID].tag);
},
error: function (error) {}
});
Run Code Online (Sandbox Code Playgroud)
这将给我以下对象,我可以轻松解析.
但是,如上所述,任何人都可以在此过程中轻松抓住我的钥匙.所以我决定将这个动作移动到我的Controller(是的,我知道这里不应该有业务逻辑,但它更安全,这是一个快速的过程).
所以我现在正在做的是运行我的Javascript,它调用Controller以获得Json返回.
使用Javascript
$.ajax({
url: "/Competitive/teamLookUp",
type: "POST",
data: "ID=" + teamID,
success: function (json) {
console.log(json);
},
error: function(error) {
}
});
Run Code Online (Sandbox Code Playgroud)
我的控制器接受了这个并尝试返回JSON.
[HttpPost]
public ActionResult teamLookUp(string ID)
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("https://na.api.pvp.net/api/lol/na/v2.3/team/" + ID + "?api_key=myKey");
myReq.ContentType = "application/json";
var response = (HttpWebResponse)myReq.GetResponse();
string text;
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
return Json(new { json = text });
}
Run Code Online (Sandbox Code Playgroud)
但是在此过程中,我返回的字符串不是JSON对象,因此我的脚本无法对其进行解析.
它将整个json作为一个长字符串返回.
此时我尝试将以下内容添加到Controller中.
var json2 = JsonConvert.DeserializeObject(text);
return Json(new { json = json2 });
Run Code Online (Sandbox Code Playgroud)
但所有返回的都是一些空的Object.
在过去的4个小时里,我一直在试错,搜索和猜测.我不知道该怎么办了.我只是希望我的Controller传回一个像这样可以再次读取的Object.(或至少某种格式化的json对象)
$.ajax({
url: "/Competitive/teamLookUp",
type: "POST",
data: "ID=" + teamID,
success: function (json) {
console.log(json);
console.log(json[teamID].name);
console.log(json[teamID].fullId);
console.log(json[teamID].roster.ownerId);
console.log(json[teamID].tag);
},
error: function (error) {}
});
Run Code Online (Sandbox Code Playgroud)
您的方法似乎不需要是a,POST
因为它只是获取数据而不是对其进行修改。因此,您可以将其设置为a GET
。
例
[HttpGet]
public JsonResult teamLookUp(string ID)
{
// Your code
return Json(text, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
最终使用WebClient
[HttpPost]
public ActionResult teamLookUp(string ID)
{
string text = "";
try
{
using (var webClient = new System.Net.WebClient())
{
webClient.Encoding = Encoding.UTF8;
var json2 = webClient.DownloadString("https://na.api.pvp.net/api/lol/na/v2.3/team/" + ID + "?api_key=myKey");
return Json(json2);
}
}
catch (Exception e)
{
text = "error";
}
return Json(new { json = text });
}
Run Code Online (Sandbox Code Playgroud)
我像平常一样解析它,
$.ajax({
url: "/Competitive/teamLookUp",
type: "POST",
data: "ID=" + ID,
dataType: "json",
success: function (resp) {
if (resp["json"] == "error") {
// error reaching server
} else {
// successfully reached server
}
json = JSON && JSON.parse(resp) || $.parseJSON(resp);
var userID = ID;
teamName = json[userID].name;
teamID = json[userID].fullId;
teamCPT = json[userID].roster.ownerId;
teamTag = json[userID].tag;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
// error
}
});
Run Code Online (Sandbox Code Playgroud)