标签: jsonp

AJAX跨域调用

我知道AJAX跨域策略.因此,我不能仅通过ajax HTTP请求调用" http://www.google.com "并在我的网站上的某处显示结果.

我尝试使用dataType"jsonp",它实际上会工作,但我得到一个语法错误(显然因为收到的数据不是JSON格式化)

是否还有其他可能从外部域接收/显示数据?iFrame遵循相同的政策?

javascript ajax jquery json jsonp

65
推荐指数
4
解决办法
14万
查看次数

使用jsonp内容类型的jQuery.ajax请求后的parsererror

我使用jQuery版本1.5.1来执行以下ajax调用:

$.ajax({
    dataType: 'jsonp',
    data: { api_key : apiKey },
    url: "http://de.dawanda.com/api/v1/" + resource + ".json",
    success: function(data) { console.log(data); },
    error: function(jqXHR, textStatus, errorThrown) { console.log(errorThrown); console.log(textStatus); }
});
Run Code Online (Sandbox Code Playgroud)

服务器使用有效的json对象进行响应:

{
  "response": {
    "type":"category",
    "entries":1,
    "params":{
      "format":"json",
      "api_key":"c9f11509529b219766a3d301d9c988ae9f6f67fb",
      "id":"406",
      "callback":"jQuery15109935275333671539_1300495251986",
      "_":"1300495252693"
    },
    "pages":1,
    "result":{
      "category":{
        "product_count":0,
        "id":406,
        "restful_path":"/categories/406",
        "parent_id":null,
        "name":"Oberteile"
       }
     }
   }
 }
Run Code Online (Sandbox Code Playgroud)

但是从不调用成功回调,而是错误回调产生了这个输出:

jQuery15109935275333671539_1300495251986 was not called
parsererror
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

我没有使用jQuery的额外库.

编辑:

如果我尝试使用"json"作为dataType而不是"jsonp"进行ajax调用,则服务器以空字符串响应.

jquery jsonp parse-error jquery-1.5

57
推荐指数
3
解决办法
9万
查看次数

简单的jQuery,PHP和JSONP示例?

我面临同源政策问题,通过研究这个主题,我发现我的特定项目的最佳方式是使用JSONP来做跨源请求.

我一直在阅读IBM关于JSONP的这篇文章,但是我并不是100%明白发生了什么.

我在这里要求的只是一个简单的jQuery> PHP JSONP请求(或任何术语可能;)) - 这样的事情(显然它是不正确的,它只是让你可以了解我想要实现的目标) :)):

jQuery的:

$.post('http://MySite.com/MyHandler.php',{firstname:'Jeff'},function(res){
    alert('Your name is '+res);
});
Run Code Online (Sandbox Code Playgroud)

PHP:

<?php
  $fname = $_POST['firstname'];
  if($fname=='Jeff')
  {
    echo 'Jeff Hansen';
  }
?>
Run Code Online (Sandbox Code Playgroud)

我如何将其转换为适当的JSONP请求?如果我要将HTML存储在要返回的结果中,那还能工作吗?

php ajax jquery json jsonp

53
推荐指数
4
解决办法
11万
查看次数

如何使用类型:在jsonp ajax调用中"POST"

我正在使用JQuery ajax jsonp.我有以下JQuery代码:

 $.ajax({  
        type:"GET",        
        url: "Login.aspx",  // Send the login info to this page
        data: str, 
        dataType: "jsonp", 
        timeout: 200000,
        jsonp:"skywardDetails",
        success: function(result)
        { 
             // Show 'Submit' Button
            $('#loginButton').show();

            // Hide Gif Spinning Rotator
            $('#ajaxloading').hide();  
         } 

    });  
Run Code Online (Sandbox Code Playgroud)

上面的代码工作正常,我只想将请求发送为"POST"而不是"GET",请建议我如何实现这一点.

谢谢

jquery jsonp http-post

52
推荐指数
3
解决办法
10万
查看次数

缺少CORS标题"Access-Control-Allow-Origin"

我从我的asp.net表单中调用此函数,并在调用ajax时在firebug控制台上收到以下错误.

跨源请求已阻止:同源策略禁止在http://anotherdomain/test.json中读取远程资源.(原因:缺少CORS标题'Access-Control-Allow-Origin').

var url= 'http://anotherdomain/test.json';
        $.ajax({
            url: url,
            crossOrigin: true,
            type: 'GET',
            xhrFields: { withCredentials: true },
            accept: 'application/json'
        }).done(function (data) {
            alert(data);                
        }).fail(function (xhr, textStatus, error) {
            var title, message;
            switch (xhr.status) {
                case 403:
                    title = xhr.responseJSON.errorSummary;
                    message = 'Please login to your server before running the test.';
                    break;
                default:
                    title = 'Invalid URL or Cross-Origin Request Blocked';
                    message = 'You must explictly add this site (' + window.location.origin + ') to the list …
Run Code Online (Sandbox Code Playgroud)

ajax jquery json jsonp cors

52
推荐指数
3
解决办法
18万
查看次数

如何在php json_decode中解决JSON_ERROR_UTF8错误?

我正在尝试这段代码

$json = file_get_contents("http://www.google.com/alerts/preview?q=test&t=7&f=1&l=0&e");
print_r(json_decode(utf8_encode($json), true));

        //////////////

// Define the errors.
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name => $value) {
    if (!strncmp($name, "JSON_ERROR_", 11)) {
        $json_errors[$value] = $name;
    }
}

// Show the errors for different depths.
foreach (range(4, 3, -1) as $depth) {
    var_dump(json_decode($json, true, $depth));
    echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多函数,html_entities_decode,utf8_encode和解码,解码十六进制代码,但我总是得到错误"JSON_ERROR_UTF8".

我该怎么解决这个问题?

php parsing json jsonp

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

JSONP调用显示"Uncaught SyntaxError:Unexpected token:"

这是我的代码

$.ajax({
        url: 'https://api.flightstats.com/flex/schedules/rest/v1/json/flight/AA/100/departing/2013/10/4?appId=19d57e69&appKey=e0ea60854c1205af43fd7b1203005d59&callback=?',
        dataType: 'JSONP',
        jsonpCallback: 'jsonCallback',
        type : 'GET',
        async: false,
        crossDomain: true,
        success: function(data) {
            console.log(data);
        }
    });
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我应该在这里添加或更改任何内容吗?任何帮助,将不胜感激.谢谢

jquery jsonp

47
推荐指数
2
解决办法
10万
查看次数

如何对Google Maps API进行跨域AJAX调用?

我正在尝试对Google Maps Geocoding Web服务进行jQuery $.getJSON调用,但由于跨域安全问题,这不起作用.

我无法在网上弄明白,但我已经阅读了一些关于Google Javascript API或JSONP的内容,但到目前为止还没有明确答案......

谁能让我高兴?

谢谢!

javascript google-maps jsonp cross-domain google-maps-api-3

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

jquery如何在另一个结束后使用多个ajax调用

我在移动应用程序中,我使用多个Ajax调用从Web服务器接收数据,如下所示

function get_json() {
    $(document).ready(function() {
        $.ajax({
            url: 'http://www.xxxxxxxxxxxxx',
            data: {
                name: 'xxxxxx'
            },
            dataType: 'jsonp',
            //jsonp: 'callback',
            //jsonpCallback: 'jsonpCallback',
            success: function(data) {
                $.each(data.posts, function(i, post) {
                    $.mobile.notesdb.transaction(function(t) {
                        t.executeSql('INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount, pastpayments, todaypayments, paydate, receiptno) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);', [post.Id, post.Code, post.Address, post.Name, post.Description, post.EntrySeason, post.Period, post.Revenue, post.PastPayments, post.todaypayments, post.paydate, post.receiptno],
                        //$.mobile.changePage('#page3', 'slide', false, true),  
                        null);
                    });
                    $('#mycontent').append(post.Name);
                });
            }
        });

        $.ajax({
            xxxx
        });

        $.ajax({
            xxxx
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

如何强制第二次ajax呼叫在第一次结束后开始...第二次结束后第三次继续?

jquery jsonp get function

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

AJAX调用和清理JSON但语法错误:缺失; 在声明之前

我正在使用以下代码进行跨域JSONP调用:

jQuery.ajax({
        async: true,
        url: 'http://mnews.hostoi.com/test.json',
        dataType: 'jsonp',
        method: "GET",
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(textStatus + ': ' + errorThrown);
        },
        success: function (data, textStatus, jqXHR) {
            if (data.Error || data.Response) {
                exists = 0;
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

在Firebug中调试时,我收到以下错误:

在此输入图像描述

SyntaxError: missing ; before statement
Run Code Online (Sandbox Code Playgroud)

但是,当我通过像jsonlint.com这样的工具传递我的json对象(通过JQ代码中的链接可用)时,它说它是有效的JSON.我也没有发现任何异常现象.怎么会返回语法错误?它是一些JSONP细节我没有得到或什么?

JSON示例

{"news":[ {
  "sentences": [
    "Neuroscientists have discovered abnormal neural activity...", 
    "The researchers found that these mice showed many symptoms...", 
    "\"Therefore,\" the study authors say, \"our findings provide a novel.."
  ], 
  "summaryId": "ZJEmY5", …
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery json jsonp

42
推荐指数
2
解决办法
6万
查看次数