我正在使用OutBrain API,我正在尝试从服务器获取一些数据。
我创建了一个简单的HTML页面,只需要获取营销商ID并返回他的名字。
当我点击按钮时 - 什么都没发生。有人能告诉我我的问题是什么吗?
我正在使用visual studio 2013并使用MVC ASP.NET。
谢谢!
代码:
index.cshtml
@{
ViewBag.Title = "Home Page";
}
<br />
<div class="details">
<label>Enter Marketer ID</label>
<input type="text" class="form-control" id="marketerID">
<button type="button" class="btn btn-default" id="buttonB">OK</button>
<section class="details">
<div class="name">
<label>Name</label>
<label id="nameMarketer"></label>
</div>
</section>
</div>
<script src="~/Scripts/outBrain.js"></script>
outBrain.js
$("#buttonB").on('click', function () {
function getMarketer ()
{
$.ajax(
{
url: "/home/GetOutBrainMarketers",
data: { id: $("marketerID").val() },
success: function (result) {
var marketer = JSON.parse(result);
document.getElementById("nameMarketer").innerHTML = (marketer.name);
}
}
)
}
});
HomeController.cs
namespace OutBrain_test.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public async Task<string> GetOutBrainMarketers(string ID)
{
var baseAddress = new Uri("https://api.outbrain.com/amplify/v0.1/marketers/" + ID);
using (var httpClient = new HttpClient { BaseAddress = baseAddress })
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("OB-TOKEN-V1", AuthorizeOutBrain().Result);
using (var response = await httpClient.GetAsync("marketers/"+ ID))
{
string responseData = await response.Content.ReadAsStringAsync();
return responseData;
}
}
}
public async Task<string> AuthorizeOutBrain()
{
var baseAddress = new Uri("https://api.outbrain.com/amplify/v0.1/login");
using (var httpClient = new HttpClient { BaseAddress = baseAddress })
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("authorization", "BASIC BASE-64-ENC(my-username:my-pass)");
using (var response = await httpClient.GetAsync("login"))
{
string responseData = await response.Content.ReadAsStringAsync();
return responseData;
}
}
}
}
}
答案 0 :(得分:2)
您的按钮JavaScript单击处理程序除了定义函数外不执行任何操作。你需要这样做:
$("#buttonB").on('click', function () {
$.ajax({ {
url: "/home/GetOutBrainMarketers",
data: { id: $("marketerID").val() },
success: function (result) {
var marketer = JSON.parse(result);
document.getElementById("nameMarketer").innerHTML = (marketer.name);
}
});
});
答案 1 :(得分:1)
您已使用按钮html控件将其替换为以下
<input type="button" class="btn btn-default" value="OK" id="buttonB" />
在outBrain.js文件中进行以下更改
function pageLoad()
{
$("#buttonB").on('click', function () {
getMarketer();
});
}
function getMarketer()
{
$.ajax({
url: "/home/GetOutBrainMarketers",
data: "{ id:'" + $("marketerID").val() +"'}",
success: function (result) {
var marketer = JSON.parse(result);
document.getElementById("nameMarketer").innerHTML = (marketer.name);
}
});
}
答案 2 :(得分:0)
$(function ()
{
$("#buttonB").click(function () {
$.ajax({
url: "/home/GetOutBrainMarketers",
data: { ID: $("marketerID").val() },
success: function (result) {
var marketer = JSON.parse(result);
document.getElementById("nameMarketer").innerHTML = (marketer.name);
}
});
});
});