从HTML页面中的第三方响应数据中获取单个值

时间:2016-04-25 05:37:57

标签: javascript html angularjs json

我需要有关如何从HTML页面获取单个值的帮助。 我在HTML页面得到了一些PAYU支付网关团队的回复,但是我需要通过处理交易

来获得个人属性值

以下是来自PAYU团队的回复:

<h1>This is the success url</h1>
<p>Your transaction is completed successfully. Bank response is 
   mihpayid=403993715514374636&amp;mode=&amp;status=failure&amp;unmappedstatus=userCancelled&amp;key=gtKFFx&amp;txnid=txn1r23fw&amp;amount=100.00&amp;discount=0.00&amp;net_amount_debit=0.00&amp;addedon=2016-04-25+10%3A48%3A29&amp;productinfo=oxygenconcentrator&amp;firstname=test&amp;lastname=&amp;address1=&amp;address2=&amp;city=&amp;state=&amp;country=&amp;zipcode=&amp;email=test%40gmail.com&amp;phone=8152709721&amp;udf1=&amp;udf2=&amp;udf3=&amp;udf4=&amp;udf5=&amp;udf6=&amp;udf7=&amp;udf8=&amp;udf9=&amp;udf10=&amp;hash=6a9d21bd423d61cd5a7d91098aa1140314e45eaddd8d4b9148399caba8ac61a9476aec130eb369f7d526e741b1b6c47b1ca71bec21fa69aa3deaa13740dbffbc&amp;field1=&amp;field2=&amp;field3=&amp;field4=&amp;field5=&amp;field6=&amp;field7=&amp;field8=&amp;field9=Cancelled+by+user&amp;payment_source=payu&amp;PG_TYPE=&amp;bank_ref_num=&amp;bankcode=&amp;error=&amp;error_Message=
</p>

 <script>
PayU.onSuccess("mihpayid=403993715514374636&amp;mode=&amp;status=failure&amp;unmappedstatus=userCancelled&amp;key=gtKFFx&amp;txnid=txn1r23fw&amp;amount=100.00&amp;discount=0.00&amp;net_amount_debit=0.00&amp;addedon=2016-04-25+10%3A48%3A29&amp;productinfo=oxygenconcentrator&amp;firstname=test&amp;lastname=&amp;address1=&amp;address2=&amp;city=&amp;state=&amp;country=&amp;zipcode=&amp;email=test%40gmail.com&amp;phone=8152709721&amp;udf1=&amp;udf2=&amp;udf3=&amp;udf4=&amp;udf5=&amp;udf6=&amp;udf7=&amp;udf8=&amp;udf9=&amp;udf10=&amp;hash=6a9d21bd423d61cd5a7d91098aa1140314e45eaddd8d4b9148399caba8ac61a9476aec130eb369f7d526e741b1b6c47b1ca71bec21fa69aa3deaa13740dbffbc&amp;field1=&amp;field2=&amp;field3=&amp;field4=&amp;field5=&amp;field6=&amp;field7=&amp;field8=&amp;field9=Cancelled+by+user&amp;payment_source=payu&amp;PG_TYPE=&amp;bank_ref_num=&amp;bankcode=&amp;error=&amp;error_Message=");
 </script>

我从以下代码段获得此回复

iabRef.executeScript(
   { code: "document.body.innerHTML" },
    function( values ) {
        alert(values[0]);
        console.log(values[0]);
    }
  );

所以我需要个别属性值,例如 mihpayid 模式状态等等......

2 个答案:

答案 0 :(得分:1)

假设值或值[0]将具有: &#34; mihpayid = 403993715514374636&安培;模式=安培;状态=失败&安培; unmappedstat&#34;

然后你可以编写如下函数:

   function extractScript(source){
     var pattern = /<script>(\w+)<\/script>/
     var matches = source.match(pattern);

     return matches[1];
   }

function getValue(source, key){
      var pattern = key+'=(\\w+)(&amp;)?';
      var expr = new RegExp(pattern);

      var result = source.match(expr);

      return result[1];
 }

然后在executeScript中执行:

iabRef.executeScript(
{ code: "document.body.innerHTML" },
  function( values ) {
   //incase values[0] contains result string
   console.log(getValue(values[0], 'mihpayid'))
   //or
   //incase values[0] contains result string
   console.log(getValue(values, 'mihpayid'))
}
);

答案 1 :(得分:0)

您可以获取p标记的内容,然后执行拆分两次

假设p是页面中唯一的p标记,您可以通过调用

获取值
 var text= document.getElementByTagName('p').innerHtml;

首先拆分&amp;

var theArray= text.split('&amp;');//or just & depending on how your text comes out 

这会返回一个数组,其中包含[status=failure,phone=8152709721]之类的内容 然后你可以遍历这个数组并创建一个对象

var obj = {} ;
//loop here then do this within the loop 
var kv=theArray[i].split('=');
obj[kv[0]] = kv[1];

因此,您可以通过调用obj.status

来获取您的属性