2017-07-18 43 views
0

我需要一些帮助。jQuery Post + MVC流程

<button id="link-button">Link Account</button> 

<script src="https://cdn.plaid.com/link/v2/stable/link-initialize.js"></script> 
<script type="text/javascript"> 
var handler = Plaid.create({ 
    clientName: 'Plaid Walkthrough Demo', 
    env: 'sandbox', 
    key: '[PUBLIC_KEY]', // Replace with your public_key to test with live credentials 
    product: ['auth', 'transactions'], 
    webhook: '[WEBHOOK_URL]', // Optional – use webhooks to get transaction and error updates 
    selectAccount: false, // Optional – trigger the Select Account 
    onLoad: function() { 
    // Optional, called when Link loads 
    }, 
    onSuccess: function(public_token, metadata) { 
    // Send the public_token to your app server. 
    // The metadata object contains info about the institution the 
    // user selected and the account ID, if `selectAccount` is enabled. 
    $.post('/plaid/exchangetoken', { 
     public_token: public_token, 
    }); 
    }, 
    onExit: function(err, metadata) { 
    // The user exited the Link flow. 
    if (err != null) { 
     // The user encountered a Plaid API error prior to exiting. 
    } 
    // metadata contains information about the institution 
    // that the user selected and the most recent API request IDs. 
    // Storing this information can be helpful for support. 
    } 
}); 

$('#link-button').on('click', function(e) { 
    handler.open(); 
    // Alternatively, you can have a specific institution 
    // prompt for authentication. Example: 
    // 
    // handler.open('ins_100000'); 
    // 
    // This will open Link with Union Bank as the institution. 
}); 
</script> 

在这里,我收到了public_token并将其发送到我的控制器,然后我在控制器换取一个的access_token。

[HttpPost] 
     public ActionResult ExchangeToken(string public_token) 
     { 
      var client = new RestClient("https://development.plaid.com/item/public_token/exchange"); 
      var request = new RestRequest(Method.POST); 
      request.AddHeader("postman-token", "c4c60478-e5c2-3ef7-5b3f-74a1d6ab871c"); 
      request.AddHeader("cache-control", "no-cache"); 
      request.AddHeader("content-type", "application/json"); 
      request.AddParameter("application/json", "{\n\t\"client_id\" : \"xxxxx\",\n\t\"public_token\" : \"" + public_token + "\",\n\t\"secret\" : \"xxxxxx\"\n}", ParameterType.RequestBody); 
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; 
      IRestResponse response = client.Execute(request); 

      var content = response.Content; 

      JToken token = JObject.Parse(content); 

      var access_token = token.SelectToken("access_token").ToString(); 
      var item_id = token.SelectToken("item_id").ToString(); 

      //---------------------------------------------------------------------------------------------- 

      var client2 = new RestClient("https://development.plaid.com/transactions/get"); 
      var request2 = new RestRequest(Method.POST); 
      request2.AddHeader("postman-token", "e55586a7-dc79-e4a8-6f1f-ac2d7030c582"); 
      request2.AddHeader("cache-control", "no-cache"); 
      request2.AddHeader("content-type", "application/json"); 
      request2.AddParameter("application/json", "{\n\t\"client_id\" : \"xxxxxxx\",\n\t\"secret\" : \"xxxxxxxxx\",\n\t\"access_token\" : \"" + access_token + "\",\n\t\"start_date\" : \"2017-01-01\",\n\t\"end_date\" : \"2017-07-07\"\n}", ParameterType.RequestBody); 
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; 
      IRestResponse response2 = client2.Execute(request2); 

      var content2 = response2.Content; 

      JToken token2 = JObject.Parse(content2); 

      var transactions = token2.SelectToken("transactions").ToString(); 

      return PartialView("Index", transactions); 
     } 

的问题是,如果你能看到ExchangeToken操作方法变量交易时要调试的观点不符合交易

回答

0

渲染,如果你有一个名为索引视图。 CSHTML与下面的代码,你应该看到的结果是:

@model串

值:@Model

+0

呀那的问题,它不显示它,我试过你说的方式 – Sergiu

+0

你的JavaScript做什么?我看不到任何你用控制器返回的部分视图内容替换html的地方。你能够看到,如果jQuery发布工作正常,没有任何错误? –

+0

是的,jquery post工作正常,我得到了public_token,正如我所说的那样,它也返回了我的交易。但是,无论我输入什么内容,它都不会显示部分视图。 – Sergiu