简体   繁体   English

jQuery从HTTP请求中获取JSON

[英]jQuery Get JSON from HTTP request

I'm trying to load JSON from other page provided by my server. 我正在尝试从我的服务器提供的其他页面加载JSON。 This is my javascript code: 这是我的javascript代码:

$('#checkprice').click(function(){
  $.get('{{route('userinventory')}}',function(data){
  console.log('test')
},'json');     
}); 

Now, this: 现在,这个:

  {{route('useriventory)}} 

Is working fine, it just generate url for me and when I visit it, this is type of data i'm getting: 工作正常,它只为我生成网址,当我访问它时,这是我得到的数据类型:

[{"assetid":"8522812062","classid":"720539092","itemname":"\u2605 Bayonet | Doppler (Factory New)","name_color":"8650AC","price":156.34},{"assetid":"8516603597","classid":"721320736","itemname":"\u2605 StatTrak\u2122 M9 Bayonet | Marble Fade (Factory New)","name_color":"8650AC","price":540}]

I think my JSON data is fine, url /userinventory hits method that just return json encoded array. 我认为我的JSON数据很好,url / userinventory命中只返回json编码数组的方法。 When I try to get it in JS it's not working for some reason. 当我试图在JS中获取它时,由于某种原因它不起作用。 First I tried json.get now tried with ajax same result (nothing is happening) 首先我尝试了json.get现在尝试使用ajax相同的结果(没有发生)

Just follow the $.get documentation of jquery. 只需按照jquery的$ .get文档即可 The second parameter is a data parameter. 第二个参数是数据参数。 So you code should work like this: 所以你的代码应该像这样工作:

    $('#checkprice').click(function(){
       $.get('{{route('userinventory')}}',{},function(data){
          console.log('test')
       },'json');     
    }); 

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

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