标签: getjson

jQuery getJSON - 返回调用函数的值

    String.prototype.getLanguage = function() {
        $.getJSON('http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=' + this + '&callback=?',
            function(json) {
               return json.responseData.language;
            });
    };
Run Code Online (Sandbox Code Playgroud)

如何将值返回给调用者值?谢谢.

编辑:我试过这个:

    String.prototype.getLanguage = function() {
        var returnValue = null;

        $.getJSON('http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=' + this + '&callback=?',
            function(json) {
               returnValue = json.responseData.language;
            });

        return returnValue;
    };
Run Code Online (Sandbox Code Playgroud)

但它也没有用.它返回null.

javascript jquery getjson

5
推荐指数
1
解决办法
1万
查看次数

为什么这个简单的jQuery getJSON不能在IE8中运行?

我有一个非常标准的AJAX请求:

$.getJSON('/products/findmatching/38647.json', {}, function(JsonData){
  var tableHtml = '';
  var x;

  for (x in JsonData.matchingProds) {
    var matchingProd = JsonData.matchingProds[x];
    var buyMessage;

    if ( x == 0 ) {
      buyMessage = 'Buy Cheapest';
    }
    else {
      buyMessage = 'Buy from this shop';
    }

    tableHtml = tableHtml + '<tr><td><img height="40" src="' + matchingProd.img_url + '" alt="' + matchingProd.name + '" /></td> \
      <td><a href="' + matchingProd._page_url + '">' + matchingProd.name + '</a></td> \
      <td><a href="' + matchingProd._merchant._url + '">' + matchingProd._merchant.title + …
Run Code Online (Sandbox Code Playgroud)

jquery getjson internet-explorer-8

5
推荐指数
1
解决办法
7527
查看次数

Access-Control-Allow-Origin 不允许 Yelp API 来源 http://localhost:8888

使用以下代码,我使用 Chrome 的 JavaScript 开发者控制台在该问题的标题中收到错误:

    jQuery.getJSON("http://api.yelp.com/business_review_search?term=starbucks&location=Urbana%20IL&limit=3&ywsid=XXX",
 function(data){
  jQuery.each(data, function(i,businesses){   
   jQuery("#yelpPreview").append(businesses.url);
   if ( i == (amount - 1) ) return false;
  });
 });
Run Code Online (Sandbox Code Playgroud)

完整的错误是: XMLHttpRequest 无法加载http://api.yelp.com/business_review_search?term=starbucks&location=Urbana%20IL&limit=3&ywsid=XXX。Access-Control-Allow-Origin 不允许来源http://localhost:8888 。

我使用 MAMP 作为我的本地主机。

这是 Yelp 阻止 API 访问本地主机的问题,还是我的代码中有错误?

api access-control getjson

5
推荐指数
1
解决办法
5541
查看次数

jQuery .getJSON返回数组变量&json数组操作


有没有办法让$ .getJSON返回变量数组?
我知道它的异步和超出范围,但我会在ajax回调中使用它,我只需要先获取所有值并检查它们与另一个数组.

就像是:

$.getJSON('itemManager.php?a=getItems', function(data){
    // itemArray = new Array(data);
    // idsArray = new Array(data.id);
    for (var i in someOtherArray){
        if($.inArray(i, idsArray) == -1){
            // do something...
            // get jason variable by id?
            // itemArray[i].someVariable
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑: JSON结构

[{"id":"786","user_id":"1","seller_id":"2","address_id":"1","time":1299852115,"publicComment":null,"personalComment":null},
{"id":"787","user_id":"1","seller_id":"2","address_id":"1","time":1299852115,"publicComment":null,"personalComment":null},
{"id":"785","user_id":"1","seller_id":"2","address_id":"1","time":1299852114,"publicComment":null,"personalComment":null},
{"id":"784","user_id":"1","seller_id":"2","address_id":"1","time":1299852113,"publicComment":null,"personalComment":null},
{"id":"783","user_id":"1","seller_id":"2","address_id":"1","time":1299852111,"publicComment":null,"personalComment":null}]
Run Code Online (Sandbox Code Playgroud)

这基本上就是这个想法.

  • 获得所有价值
  • 隔离JSON对象的id值
  • 循环另一个数组
  • 检查json id是否在另一个数组中
  • 按id值访问其他json变量

我猜这里有各种解决方案,但我正在寻找代码最少的东西.

javascript arrays jquery json getjson

5
推荐指数
1
解决办法
1万
查看次数

从web api JSON C#中删除斜杠

我有一个WEB API C#,里面我有Data这是一个链接,例如: images/Chinese/AbaloneEggCustard.jpg

但在JSON中,它看起来像这样:

[{"BackgroundImage":"images\/Chinese\/AbaloneEggCustard.jpg", ......}]
Run Code Online (Sandbox Code Playgroud)

我可以知道如何删除斜线?需要删除所以希望我可以访问与azure链接时的图像.


这是我的控制器代码:

public IEnumerable<Food> Get()
    {
        List<Food> Cases = new List<Food>();
        try
        {
            string connectionString = ConfigurationManager.ConnectionStrings["HealthyFoodDBConnectionString"].ConnectionString;
            myConnection = new SqlConnection(connectionString);
            myConnection.Open();

            string sql = "SELECT * from [Recipe] ";

            myCommand = new SqlCommand(sql, myConnection);
            myDataReader = myCommand.ExecuteReader();

            while (myDataReader.Read())
            {
                Cases.Add(new Food()
                {
                    RecipeID = (int)myDataReader["RecipeID"],
                    RecipeTitle = (string)myDataReader["RecipeTitle"],
                    FoodCategoryID = Convert.ToInt32(myDataReader["FoodCategoryId"]),
                    Serves = (string)myDataReader["Serves"],
                    PerServing = (string)myDataReader["PerServing"],
                    Favourite = ((Convert.ToInt32(myDataReader["Favourite"]) == 1) ? true : false),
                    Directions = (string)myDataReader["Directions"],
                    BackgroundImage = …
Run Code Online (Sandbox Code Playgroud)

c# json getjson asp.net-web-api

5
推荐指数
1
解决办法
4591
查看次数

在PHP的json_encode截断之前转义JSON撇号?

我在MySQL数据库(utf8_general_ci)中有一个包含卷曲(智能?)撇号的字段: Owner’s...

如果我访问从数据库中提取它的PHP页面,这打印很好,没有特殊处理.但是,我试图通过另一个页面上的$ .getJSON请求来访问它,所以我使用了PHP的json_encode.它截断值以便它读取Owner,然后成功编码其余数据.如果我在json_encode之前在字段上使用PHP的utf8_encode,它包含编码的完整值\u0092,然后在页面上不打印任何东西,给我Owners.PHP htmlentitieshtmlspecialchars没有效果.

查看Chrome工具中的请求,Owner’s显示Owner?s在$ .getJSON页面上.

有人可以帮我从这里出去吗?我已经阅读了关于SO和网络的其他问题,但我找不到任何有用的东西,而且我没有使用JSON.

谢谢阅读.

php json getjson

5
推荐指数
2
解决办法
9532
查看次数

Ember getJSON.done() 与 .then()

我正在使用 Ember,以下代码从 api.php 脚本获取 JSON 并在模板上显示结果。我的问题是,当我将 getJSON 函数更改为使用 .done() 而不是 .then() 时,为什么脚本会中断?我收到以下错误:

:未捕获错误:断言失败:Ember.CollectionView 的内容必须实现 Ember.Array。您传递了 [object Object] 。

如果我在函数期间记录 response.items 对象,我会在控制台中得到相同的结果,所以我很好奇 Ember 如何以不同的方式解释这一点。

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return App.Item.all();
  }
});

App.Item = Ember.Object.extend();

App.Item.reopenClass({
  all: function() {
      return $.getJSON("api.php").then(function(response) {
        var items = [];

        response.items.forEach( function (item) {
          items.push( App.Item.create(item) );
        });
        return items;
      });
  }
});
Run Code Online (Sandbox Code Playgroud)

javascript api jquery getjson ember.js

5
推荐指数
1
解决办法
5764
查看次数

将身份验证包含到 $.getJSON

我使用以下代码从 api 获取 json 数据:

$.getJSON( '../propertylocator/api/configurations', function(data) { 
        $.each( data.rows, function(i, rows) {
            //var homeLatlng= new google.maps.LatLng(0, -180);
            $('#map_canvas').gmap('addShape', { 
                'Polyline': new google.maps.LatLng(rows.latitude, rows.longitude), 
                'bounds': true 
            }).click(function() {
                $('#map_canvas').gmap('openInfoWindow', { 'content': '<strong><a href="detailPage.php?id='+ rows.propertyid +'">'+rows.propertyname+'</a></strong><br/>'+rows.propertyoverview }, this);
        });
});
Run Code Online (Sandbox Code Playgroud)

工作正常,但昨天我已经在 Web 应用程序中包含了带有加密密码的基本身份验证,如何在获取 json api 时包含此代码的身份验证以进行身份​​验证?

谢谢

javascript rest jquery json getjson

5
推荐指数
0
解决办法
9650
查看次数

从 Chrome 控制台重复发出 ajax 请求时出现“资源不足错误”

我有这个 javascript 代码,旨在在 google chrome 控制台内运行。它不断检查 json 格式的响应。如果响应中的任何位置 BestPrice 等于 max_price,那么它将使用某些 API 进行购买。我遇到的问题是运行大约 10 秒后,我收到“ERR_INSUFFICIENT_RESOURCES”。

我想这是因为太多的请求?我需要它尽可能快地循环,所以如果它不能即时,是否有一定的请求限制?

代码:

function snipebot(page, max_page, max_price){
    $.getJSON('http://www.roblox.com/catalog/json?browse.aspx?Subcategory=2&Keyword=&CurrencyType=0&pxMin=0&pxMax=0&SortType=2&SortAggregation=0&SortCurrency=0&LegendExpanded=true&Category=2&PageNumber=' + page, function(data){
        $.each(data, function(index, item){
            if (item['BestPrice'] <= max_price){
                $.get('http://www.roblox.com/Item.aspx?id=' + item['AssetId'], function(data){
                    var purchaseData = $($(data).find(".PurchaseButton")[0]).data();
                    if (purchaseData['expectedPrice'] <= item['BestPrice']){
                        $.post('/API/Item.ashx?rqtype=purchase&productID=' + purchaseData['productId'] + '&expectedCurrency=1&expectedPrice=' + purchaseData['expectedPrice'] + '&expectedSellerId=' + purchaseData['expectedSellerId'] + '&userAssetID=' + purchaseData['userassetId'], function(){
                            console.log('[' + item['BestPrice'] + '] @' + new Date().toTimeString())    
                        });
                    } else {
                        console.log("Detected purchase.");
                    }
                });
            };
            setTimeout(function(){
                snipebot(page …
Run Code Online (Sandbox Code Playgroud)

javascript getjson

5
推荐指数
1
解决办法
9586
查看次数

无法为 Product.class 调用无参数构造函数

我在我的 android 应用程序中使用 GSON 和 Volley 库进行网络连接,但是在使用 Gson 将 Json 响应转换为模型类时,我得到了以下错误:

88-2006/ E/Volley? [121] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Unable to invoke no-args constructor for class [Lcom.example.model.Product;. Register an InstanceCreator with Gson for this type may fix this problem.
    java.lang.RuntimeException: Unable to invoke no-args constructor for class [Lcom.example.model.Product;. Register an InstanceCreator with Gson for this type may fix this problem.
Run Code Online (Sandbox Code Playgroud)

这些是我正在使用的 POJO 类:Product.java

public class Product {

    private String status;

    private List<Result> results = new ArrayList<Result>();

    private Pagination pagination;
    public Product(){} …
Run Code Online (Sandbox Code Playgroud)

java android getjson gson

5
推荐指数
1
解决办法
2万
查看次数