【问题标题】:How to use a button to submit data, load a new page and load json data?如何使用按钮提交数据、加载新页面和加载json数据?
【发布时间】:2018-04-19 18:26:26
【问题描述】:

我正在尝试使用 api 构建一个从 api 获取 JSON 数据的应用程序,我需要在我的索引页面上有一个按钮,然后当我输入汽车注册信息时,信息会加载到另一个页面中。

我目前通过用户输入、标签和提交按钮加载信息,但我不知道如何将输入和按钮放在单独的页面上,然后加载 json 数据。这是我到目前为止所拥有的。

<form id="vehicle-smart-sample-form">
        <label for="reg">Enter your reg</label>
        <input type="text" id="reg"/>
    <a href="stats.html"><button type="submit">Search</button></a>
    </form>

$(document).ready(function () {
        $("#vehicle-smart-sample-form").submit(function (e) {
            e.preventDefault();
            var reg = $("#reg").val();
            $.ajax({
                "async": true,
                "crossDomain": true,
                "url": "https://www.getvehiclesmart.com/rest/vehicleData?reg=" + reg + "&isRefreshing=false&appid="+myAppId,
                "method": "GET",
                "headers": {
                    "Content-Type": "application/text",
                    "Accept": "application/json",
                    "Cache-Control": "no-cache"
                }
            })
          });

【问题讨论】:

    标签: javascript jquery json api


    【解决方案1】:

    我不太明白,如果你想打开新的空白页面并显示 Json 数据,你可以在 ajax 选项上使用它

    success: function(data){
    var myWindow = window.open("_blank");
    myWindow.document.write("<p>"+data.property+"</p>");//for example
    }
    

    您也可以打开“stats.html”并在其中加载Json数据。

    success: function(data){
    var myWindow = window.open("stats.html", "_blank");
    myWindow.document.getElementById("car_info").innerText = data.carinfo;//for example
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 1970-01-01
      • 2018-10-15
      • 2013-11-15
      • 2016-03-28
      • 2016-09-03
      • 1970-01-01
      • 2023-03-11
      • 2017-08-18
      相关资源
      最近更新 更多