【问题标题】:Simple ajax request简单的ajax请求
【发布时间】:2012-03-23 03:00:26
【问题描述】:

我是 ajax 新手,正在尝试使以下脚本正常工作。我只想从 json 对象中获取信息并将其打印到文档中。

这是名为 companyinfo.json 的 JSON 文件:

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


}

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);

从简单的开始。我只是控制台记录 data.id 以查看脚本是否从 json 文件返回值。

要将其写入文档,我会这样做,在上面的回调函数中调用 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;
        }


    }
}

我对 Ajax 还很陌生,但希望这有点道理。谢谢

【问题讨论】:

  • 那么...有什么问题?
  • 我应该将 company1 的值设置为什么(也就是我应该在我提交的输入字段中输入什么)。以及应该如何格式化相应的 json 文档以反映该公司名称?当我在上面提交该脚本时什么都没有发生

标签: javascript ajax json xmlhttprequest


【解决方案1】:

如果你想得到一些我认为你应该添加的东西

type:"GET" 

在你的 $.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>');
        });
    }

});

【讨论】:

    【解决方案2】:

    我不确定您的代码中是否存在这一点:

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

    应该和()这样的:

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

    【讨论】:

      【解决方案3】:

      获取和解析 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
      });
      

      【讨论】:

        【解决方案4】:

        你可以这样做:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-04-12
          • 1970-01-01
          • 2014-05-29
          • 1970-01-01
          • 1970-01-01
          • 2016-05-29
          • 1970-01-01
          相关资源
          最近更新 更多