从Angular js中的Outcoming HTTP POST响应中获取表单数据

时间:2015-04-03 09:14:46

标签: angularjs asp.net-web-api http-headers httprequest

我正在尝试使用angularjs和asp.net web api开发支付模块。 我正在使用付款api(iyzico)。

使用该API我创建购物令牌请求,此请求返回一个js脚本响应和一个购物令牌。

我加载此js文件,并通过返回js文件生成付款表单。

在生成付款表单后,我输入了我的信用卡信息并将这些信息发送到付款api然后付款api返回响应但我无法从页面获得此响应(此响应在页面加载时出现)。

Http获取在payment.js中生成购物令牌的方法

  function loadDetails() {

        $http.get( '/API/api/profile/GetPaymentToken').then(function (results) {
            $scope.paymentData = jQuery.parseJSON(results.data);
            $scope.code_snippet = $sce.trustAsHtml($scope.paymentData.code_snippet);
            var paymentScriptUrl = "https://www.iyzico.com/frontend/form/v1/widget.js?mode=test&token=" + $scope.paymentData.transaction_token;
            $.ajax({
                url: paymentScriptUrl,
                dataType: "script",
                cache: true,
                success: function (data, textStatus, jqXHR) {
                    var f = new Function(data);
                    f();

                    iyzi_jQuery(this).iyziPayment({
                        host: 'https://iyziconnect.com/pay-with-transaction-token/',
                        mode: 'test',
                        assetsURL: 'https://www.iyzico.com/frontend/form/v1/',
                        installment: '0',
                        language: 'tr'
                    });

                },
                error: function (jqXHR, status, err) {
                    console.log("");
                }
            });
        },
            function (response) { // optional
                _helper.error.handleError(toaster, response.data, response.status);
            })

    }

Web.Api中的令牌生成器,在payment.js中调用

[Authorize]
[HttpGet]
[Route("GetPaymentToken")]
public async Task<IHttpActionResult> GetPaymentToken()
{
    System.Diagnostics.Debugger.Break(); 
    WebRequest request = WebRequest.Create("https://api.iyzico.com/v2/create");
    request.Method = "POST";
    var rand = new Random();
    var external_id = rand.Next(0, 100000000);
    string postData = "api_id=my_id"
    + "&secret=my_secret"
    + "&external_id=" + external_id.ToString()
    + "&mode=test"
    + "&type=CC.DB"
    + "&return_url=http://localhost:2020/payment2" //returning url
    + "&amount=10020"
    + "&currency=TRY"
    + "&descriptor=PAYMENT_DESCRIPTION"
    + "&customer_contact_ip=CUSTOMER_IP"
    + "&customer_language=tr"
     + "&installment=false";

    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = byteArray.Length;
    System.IO.Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    WebResponse response = request.GetResponse();
    dataStream = response.GetResponseStream();

    System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();

    reader.Close();
    dataStream.Close();
    response.Close();

    return Ok(responseFromServer);

获得购物令牌后,我输入我的信用卡信息并将表格发送到外部付款api和外部api返回信息到我的返回URL(http://localhost:2020/payment2),关于我的付款成功或失败。

我可以在Chrome开发者工具中看到来自外部API的返回响应&gt;网络操作,但我无法从mycontroller js文件中获取该表单数据。返回响应低于

Remote Address:[::1]:2020
Request URL:http://localhost:2020/payment2
Request Method:POST
Status Code:200 OK
Response Headers
view source
Cache-Control:private
Content-Length:11337
Content-Type:text/html
Date:Fri, 03 Apr 2015 08:45:22 GMT
Server:Microsoft-IIS/8.5
X-Powered-By:ASP.NET
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6,tr;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:532
Content-Type:application/x-www-form-urlencoded
Cookie:countrySCJS=TR; ootdcuSCJS=34602; ootdchSCJS=10742; ctxjs1420m06d05=7b2273756363657373223a302c226c6f675f616374697665223a317d; ASPSESSIONIDQCDBDRBQ=BKGJHHKCKDDLJCBBPMPJJHFH; ASPSESSIONIDQAADBTBR=AMPJDALCPFGILMAOKEDMMGBP; ASPSESSIONIDSCAADQAR=GKKFJCCDLILKPNHAEHBHHLHJ
Host:localhost:2020
Origin:null
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36
Form Data
view source
view URL encoded
**json:{"response":{"state":"failed","date_time":"15-04-03 08:43:24","error_code":"800.100.154","error_message":"transaction marked as invalid","error_message_tr":""},"transaction":{"transaction_id":"MTQyODA1MDU4NAnrin0vdEV06f4hWARX","external_id":"23583443","reference_id":"____________20150403084323.653059Kq","state":"rejected","connector_type":"Isbank","installment_count":0}}**

如何将json命名为array?

1 个答案:

答案 0 :(得分:0)

您必须牺牲您的单页应用程序才能进行回拨。

  1. 获取回调到您的REST地址
  2. 处理POST参数
  3. 生成(返回text / html)FAIL页面的简单成功。让此页面包含结果并链接到相应的页面(主页面网址或付款页面网址)
  4. 链接应该打开您的单页angularjs,您的控制器应该显示 根据网址提供适当的页面。