【问题标题】:Inserting data from Google Search API into MySQL Database将来自 Google Search API 的数据插入 MySQL 数据库
【发布时间】:2017-04-27 11:28:48
【问题描述】:

我希望大家能帮助我并理解我的问题。正如您在此处看到的,我想从谷歌搜索 API 获取数据并保存到我的数据库中并显示它。我已经使用了ajax。 html,但我得到了错误。 我要保存的数据是 sn-p ,标题和链接。我不想保存我从中获得的所有数据。我认为我的问题是如何将变量从 javascript 传递到 mysql

<html>
<head>
<title>Google Search API</title>
</head>
<body>
<textarea id="myTextarea"></textarea>
<button type="button" id="mySubmit" onclick="myFunction()" >Search</button>
<div id="content">
<div id="content1">
</div>
<script id="searchme"></script>
<script>

    function hndlr(response) 
            {
                for (var i = 0; i <response.items.length; i++) 
                        {
                            var item = response.items[i];     
                            var api = document.getElementById("content").innerHTML +="<br>"+"<b>"+ item.title + "</b>"+"<br>" +item.snippet  + "<br>"+"<a href='"+item.link+"'>"+item.link+"</a>";
                            var link = item.link; 
                            var snippet =item.snippet;
                            var title =item.title;


                        }
            }

    function myFunction() 
            {
                var query = document.getElementById("myTextarea").value;
                var cx ='004123968310343535430%3Aaxml6iel9yo'
                var key ='AIzaSyDxPcphnrcN9_dfkRkFTbwkv44m1-HI1Hg'
                var search_query = "https://www.googleapis.com/customsearch/v1?callback=hndlr&sort=date&cx="+cx+"&key="+key+"&q="+query;
                document.getElementById('searchme').src = search_query; 
            }


</script>
</body>
</html>

我之前已经发布过这个问题,但有人说它已经重复了。但我认为他根本不明白我的问题。如果你先测试这个 php,我想你明白我想要什么

【问题讨论】:

    标签: javascript jquery ajax api mysqli


    【解决方案1】:

    我认为您需要在使用回调函数获得结果后进行技巧。

    function myCallBackCustomFunction(callback) 
           {
               //DO YOUR STUFF FOR api then call your server to store data    
    
              yourCustomsaveFunction(callback);
           }
    function yourCustomsaveFunction(googlevars)
    {
     $.ajax("your url?myparameters="+googlevars, {
      success: function(data) {
        //your data save msg if any
      },
      error: function() {
         //your error msg if any
      }
    

    }); }

    如果您愿意,您可以将 ajax 方法从 GET 更改为 POST。

    【讨论】:

    • 其实我的myFunction里已经有回调函数了,还需要再做一次吗?
    • 不...只需从回调函数中获取结果并将其传递到您的服务器以保存结果。
    • 你是什么意思?可以举个例子吗?
    • @Rushdiey 我已经更新了我的答案。我希望你能看得更清楚。
    • function myCallBackCustomFunction(callback) { var title =item.title;你的自定义保存函数(回调); } function yourCustomsaveFunction(googlevars) { $.ajax("ajax.php?title="+googlevars, { success: function(data) { //你的数据保存 msg if any }, error: function() { //你的错误msg if any } 是这样的吗?
    猜你喜欢
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 2023-04-10
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    相关资源
    最近更新 更多