Get html element of an AJAX request with JSONP

0

I am making a request with ajax and javascript, the answer of this request is a complete html page, of this answer I need to obtain the value of an element <input type="hidden" name="key_solicitud" value="este es el valor que deseo obtener"> , I have tried with the following code.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Documento 1</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
    <p id="html_response"></p>
    <script>
    $(document).ready(function(){
        $.ajax(
        {
            type : 'GET',
            crossDomain:true,
            url: 'http://cartago.lllf.uam.es/grampal/grampal.cgi?m=etiqueta',
            dataType: 'jsonp',
            jsonp: 'callback',
            callback: function(result) {
                var mydata = $(result).filter('key_solicitud').value();
                $('#html_response').html(mydata);
            }
        });
    })
    </script>
</body>
</html>

And several variants like find by filter (), $ (result) .filter ('key_solicitud'). text (); but I always have an error when getting the result

Uncaught SyntaxError: Unexpected token

    
asked by KevthoC 05.12.2018 в 00:12
source

1 answer

0

You need to first take your page as text, then pause it and convert it to html , once converted, we have to go through all the created elements and find the one with the corresponding name that you are looking for, and that object take value .

I think there are better ways to do it, this is just an example that can be improved.

This is the example:

   <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Documento 1</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    </head>
    <body>
        <p id="html_response"></p>
        <script>
        $(document).ready(function(){
            $.ajax(
            {
                type : 'GET',
                crossDomain:true,
                url: 'http://cartago.lllf.uam.es/grampal/grampal.cgi?m=etiqueta',
                dataType: 'html',
                jsonp: 'callback',
                success: function(result) {
                    var mydata = $.parseHTML(result);
                    for(var i = 0; i < mydata.length; i++){
                       if (mydata[i].name == "key_solicitud") {
                          $('#html_response').html(mydata[i].value);
                       }
                    }
                }
            });
        })
        </script>
    </body>
    </html>

I hope it serves you.

    
answered by 05.12.2018 в 02:12