通过Ajax传递POST数据

huangapple go评论58阅读模式
英文:

Passing post via Ajax

问题

以下是翻译好的内容:

表单页面:

<script>
function metatags() {
    var url = $('#url').val;
    jQuery.ajax({
        url: "inc/recoverData.php",
        type: "POST",
        data: 'url=' + url,
        dataType: 'JSON',
        success: function(html) {
            $('#domain').val(html.domain);
            $('#title').val(html.title);
            $('#description').val(html.description);
        }
    })
}
</script>

PHP页面:

<?php

$url = 'https://www.stackoverflow.com'; // WORKS
$url = 'https://www.' . $_POST['url']; // DOESN'T WORK

$metaTags = get_meta_tags($url);
$description = $metaTags['description'];
$domain = parse_url($url, PHP_URL_HOST);
$title = getTitle($url);

$data = [
    "description" => $description,
    "domain" => $domain,
    "title" => $title
];

echo json_encode($data);

If I pass url via Ajax, script doesn't work
英文:

I need to autocomplete a form with some metadata according to the URL provided.

Form page:

&lt;script&gt;
function metatags() {
    var url = $(&#39;#url&#39;).val;
    jQuery.ajax({
        url: &quot;inc/recoverData.php&quot;,
        type: &quot;POST&quot;,
        data: &#39;url=&#39; + url,
        dataType: &#39;JSON&#39;,
        success: function(html) {
            $(&#39;#domain&#39;).val(html.domain);
            $(&#39;#title&#39;).val(html.title);
            $(&#39;#description&#39;).val(html.description);
        }
    })
}
&lt;/script&gt;

PHP page

    &lt;?php

    $url = &#39;https://www.stackoverflow.com&#39;; // WORKS
    $url = &#39;https://www.&#39; . $_POST[&#39;url&#39;]; //  DOESNT WORKS
    
    $metaTags = get_meta_tags($url);
    $description = $metaTags[&#39;description&#39;];
    $domain = parse_url($url, PHP_URL_HOST);
    $title = getTitle($url);
    
    $data = [
        &quot;description&quot; =&gt; $description,
        &quot;domain&quot; =&gt; $domain,
        &quot;title&quot; =&gt; $title
    ];
    
    echo json_encode($data);

If I pass url via Ajax, script doesn't works

答案1

得分: 2

  1. 你正在传递错误的数据。在ajax中,以对象的形式传递数据
  2. 你在使用val获取值。而在Jquery中,可以使用val()来获取值。

以下是更新后的代码:function metatags() { var url = $('#url').val(); jQuery.ajax({ url: "inc/recoverData.php", type: "POST", data: {url: url}, dataType: 'JSON', success: function(html) { $('#domain').val(html.domain); $('#title').val(html.title); $('#description').val(html.description); } }) }。你可以在JQuery文档中阅读更多。

英文:
  1. You are passing data incorrect. Pass the data as an object in ajax.
  2. You are using val for value. While in Jquery, the value can be get with val().

Here is the updated Code:-

function metatags() {
    var url = $(&#39;#url&#39;).val();
    jQuery.ajax({
        url: &quot;inc/recoverData.php&quot;,
        type: &quot;POST&quot;,
        data: {url: url},
        dataType: &#39;JSON&#39;,
        success: function(html) {
            $(&#39;#domain&#39;).val(html.domain);
            $(&#39;#title&#39;).val(html.title);
            $(&#39;#description&#39;).val(html.description);
        }
    })
}

You can read more in JQuery Docs.

huangapple
  • 本文由 发表于 2023年5月14日 20:10:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定