简体   繁体   中英

Parsing html page by using jQuery

I have to get a value from ajax response (html). Below is my ajax call,

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)

<!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.

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,

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.

Since the value defined in inside javascript function I'm not able to fetch that session id parameter. ie 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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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