简体   繁体   English

简单的ajax请求

[英]Simple ajax request

I am new to ajax and am trying to get the following script working. 我是ajax的新手,正在尝试使以下脚本正常工作。 I just want to take info from a json object and print it to the document. 我只想从json对象获取信息并将其打印到文档中。

Here is the JSON file called companyinfo.json: 这是一个名为companyinfo.json的JSON文件:

{
'id':1,
'name':'Stack'


}

The Ajax request looks like this: Ajax请求看起来像这样:

ar xhr = false;
var xPos, yPos;

$(function(){

    var submitButton = $("#dostuff");
    submitButton.onclick = sendInfoRequest;

});

function sendInfoRequest (evt) {
    if (evt) {
        var company1 = $("#companyInput1").val;
        var company2 = $("#companyInput2").val;
    }
    else {
        evt = window.event;
        var company = evt.srcElement;
    }
    $.ajax({
        url : 'companyinfo.json',
        dataType: 'json',
        data: company1,
        success: function(data) {
            console.log(data);
            var items = new Array ();
            $.each(data, function(key, val) {
                items.push('<li id="' + key + '">' + val + '</li>');
            });
        }

    });


    return false;
}
console.log(data.id);

To start simple. 从简单开始。 I just console.log the data.id to see if the script returned a value from the json file. 我只是console.log data.id,以查看脚本是否从json文件返回了值。

To write it to the document I would do something like this, calling the showContents function in the callback function above: 要将其写入文档,我将执行以下操作,在上述回调函数中调用showContents函数:

function showContents(companyNumber) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var outMsg = xhr.responseXML;
            $("." + data.companyName.toLowerCase + companyNumber).innerHTML(data.companyName)
        }
        else {
            var outMsg = "There was a problem with the request " + xhr.status;
        }


    }
}

I'm pretty new to Ajax, but hopefully that makes some sense. 我对Ajax来说还很陌生,但是希望这很有道理。 Thanks 谢谢

if you are trying to get something i think you should add 如果您想得到一些东西,我认为您应该添加

type:"GET" 

on your $.ajax it should look like this. 在$ .ajax上应该看起来像这样。

$.ajax({
    url : 'companyinfo.json',
    dataType: 'json',
    type:"GET",
    contentType: "application/json; charset=utf-8",
    data: company1, //What is your purpose for adding this?
    success: function(data) {
        console.log(data);
        var items = new Array ();
        $.each(data, function(key, val) {
            items.push('<li id="' + key + '">' + val + '</li>');
        });
    }

});

Im not sure about this in your code: 我不确定代码中的内容:

var company1 = $("#companyInput1").val; //should it be with ()??
var company2 = $("#companyInput2").val; //should it be with ()??

should it be with () like this one: 应该与()像这样:

var company1 = $("#companyInput1").val();
var company2 = $("#companyInput2").val();

The easiest way to get and parse JSON is to use $.getJSON 获取和解析JSON的最简单方法是使用$ .getJSON

// you need to use a map as your data => {key : value}
$.getJSON("companyinfo.json", {company : company1}, function(data){
  console.log(data);
  var items = []; // new Array();
  $.each(data, function(key, val){
    items.push('<li id="' + key + '">' + val + '</li>');
  });
  // do something with items
});

you can do like this: 您可以这样:

 $.getJSON("getJson.ashx", { Index: 1 }, function (d) {
                    alert(d);
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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