简体   繁体   English

使用jQuery解析HTML页面

[英]Parsing html page by using jQuery

I have to get a value from ajax response (html). 我必须从ajax响应(html)获取值。 Below is my ajax call, 以下是我的ajax电话,

var result = $.ajax({
                //datatype : "application/json",
                type: "POST",
                url: ddcUrl,
                data: {Bin:cardNumber, JWT:encodedJWT},
                complete: function(event){
                    console.log(event);
                }
            });

And below is my response (in HTML format) 以下是我的回复(HTML格式)

<!DOCTYPE html>
<html>
<head>
    <title>Cardinal DDC Sim</title>
</head>
<body>

<script>

    sendNotification(true, "28eb7c91-699c-4115-b25e-1ba2dc513e3e");

    function sendNotification(status, sessionId){
        try{
            var message = {
                MessageType: 'profile.completed',
                SessionId: sessionId,
                Status: status
            };
            window.parent.postMessage(JSON.stringify(message), '*');
        } catch(error){
            console.error('Failed to notify parent', error)
        }
    }
</script>
</body>
</html> 

I need to get the value 28eb7c91-699c-4115-b25e-1ba2dc513e3e in inside of javascript function. 我需要在javascript函数内部获取值28eb7c91-699c-4115-b25e-1ba2dc513e3e

The request supposed to be happened in iframe so that response (session id inside) will be added in window parameter by using below function some thing like, 该请求应该发生在iframe中,因此,通过使用以下功能,响应(内部会话ID)将添加到window参数中,例如,

window.addEventListener("message", function(event) {  

       var data = JSON.parse(event.data);

       if (data !== undefined && data.Status) {
           // Extract the value of `SessionId` for onward processing.
       }

}, false);

But because of some other issue I'm sending request by using ajax. 但是由于其他一些问题,我正在使用ajax发送请求。

Since the value defined in inside javascript function I'm not able to fetch that session id parameter. 由于在javascript函数内部定义的值,因此我无法获取该会话ID参数。 ie 28eb7c91-699c-4115-b25e-1ba2dc513e3e. 28eb7c91-699c-4115-b25e-1ba2dc513e3e。 Is there any solution to get this ? 有什么解决办法吗?

basic regular expression should be able to match the pattern from the text. 基本的正则表达式应该能够匹配文本中的模式。

$.ajax({
  type: "POST",
  url: ddcUrl,
  data: {Bin:cardNumber, JWT:encodedJWT},
  dataType: "text",
})
.done(function( data ) {
   var code = data.match(/sendNotification\(true, "([a-f0-9\-]+)"\)/)
   console.log(code);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM