jQuery的:如何启用timeout的$.ajax({dataType:'jsonp'...?有什么解决方案吗?http://jsfiddle.net/laukstein/2wcpU/4
$.ajax({
type:"GET",
url:'http://lab.laukstein.com/ajax-seo/.json',
dataType:'jsonp',
timeout:200, // Not working with dataType:'jsonp'
success:function(data){$('#content').html(data.content);},
error:function(request,status,error){$('#content').html('request failed');}
});
Run Code Online (Sandbox Code Playgroud)
我不喜欢使用一些插件,比如http://code.google.com/p/jquery-jsonp.
更新1:
如果我打字,这就是我在浏览器中得到的内容
http://www.remote_host.com/feed.php?callback=jsonpCallBack
{
"rss": {
"channels": [
{
"title": "title goes here",
"link": "http://www.remote_server.com/feed.php",
"description": "description goes here",
"items": [
{
"title": "item title goes here",
"link": "item link goes here",
"pubDate": "item date goes here",
"description": "item description goes here"
},
{
"title": "item title goes here",
"link": "item link goes here",
"pubDate": "item date goes here",
"description": "item description goes here"
},
{
"title": "item title goes here",
"link": "item link goes here",
"pubDate": "item date goes …Run Code Online (Sandbox Code Playgroud) 我正在使用他们的OData API为Netflix开发一个应用程序.我已经关注了Stephen Walther关于如何查询OData API 的博客文章.在其中,他使用以下代码:
$.ajax({
dataType: "jsonp",
url: query,
jsonpCallback: "callback",
success: callback
});
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我需要使用OData的分页链接来检索完整的列表.我的代码如下:
// create url and handle ajax call to Netflix
function getTitles() {
query = "http://odata.netflix.com/v2/Catalog" // netflix odata base url
+ "/Genres('Television')" // select Genre
+ "/Titles" // top-level resource
+ "?$select=NetflixApiId,Name,BoxArt,Synopsis,ReleaseYear,AverageRating,Series" // choose fields
+ "&$orderby=Name" // Sort results by name
+ "&$filter=Instant/Available eq true" // filter by instant view
+ " and Type eq 'Season'" // select only seasons …Run Code Online (Sandbox Code Playgroud) 我有一个像这样简单的ajax调用:
$.ajax({
url: u, type: "POST", dataType: "json",
data: data,
success: function (d) { response($.map(d, function (o) { return { label: o.Text, value: o.Text, id: o.Id} })); }
});
Run Code Online (Sandbox Code Playgroud)
它是tb自动完成的一部分,不仅适用于一个视图.它不起作用的原因是它代替json,它使jsonp请求(通过嗅探我看到它调用传递的url ?callback=jQueryxxxxxxxxx),并且永远不会调用success函数,因为jquery将它打包到匿名函数中,其名称在callback参数中传递,和服务器返回标准的json(我不想使用jsonp,因为它是POST请求而不是跨域请求).我检查了,当前的视图url和这个uajax url参数都打开了http://localhost:8080/myapp/areax/...,所以我不明白为什么jQuery在这里发出JSONP请求.
编辑:
这个不起作用的视图有url请求是这样的: http:// hostname:8080/AreaName/Report/ViewReport 和ajax的参数就像是/ AreaName/MyAutoComplete/Search,所以完整的url到哪个autocomplete是make就像 http:// hostname:8080/AreaName/MyAutoComplete/Search?callback = jQuery151013129048690121925_1327065146844
服务器的响应如下所示:
[{"Id":2,"Text":"001"},{"Id":7,"Text":"002"}]
Run Code Online (Sandbox Code Playgroud)
我知道它不是jsonp,因为它应该是
<script>
jQuery151013129048690121925_1327065146844([{"Id":2,"Text":"001"},{"Id":7,"Text":"002"}]);
</script>
Run Code Online (Sandbox Code Playgroud)
但我想制作正常的json请求,而不是jsonp.
UPDATE
最奇怪的事情(我开始认为这是jquery v1.5.1中用于项目的一个错误)是当我删除时dataType: "json",它会产生一个正常的json请求:)
所以,不是如何制作json请求,现在我将接受一个解释为什么它按预期工作(并且dataType:"json"的那个没有):
$.ajax({
url: u, type: "POST",
data: data,
success: function (d) { response($.map(d, function (o) { …Run Code Online (Sandbox Code Playgroud) 我有一个MVC应用程序,我正在使用各种JsonResult端点来填充javascript ViewModel.
我一直在使用几个jQuery Ajax请求来填充模型,但我希望将尽可能多的初始模型传递给服务器上的视图.
ViewModel有3-5个部分(取决于用户在应用程序中的位置):
我目前正在使用此代码加载前三个部分:
$(document).ready(function () {
ko.applyBindings(viewModel);
@Html.Raw(ViewBag.Script)
// Piece 1. Almost always the same thing
postJSON('@Url.Action("HomeViewModelJson", "Home")', function (data) {
if (data == null)
return;
for (var i in data.Tabs) {
viewModel.tabs.push({ name: data.Tabs[i] });
}
for (var i in data.Buttons) {
viewModel.metroButtons.push({ name: data.MetroButtons[i] });
}
for (var i in data.Ribbons) {
viewModel.ribbons.push(data.Ribbons[i]);
}
ApplyButtonThemes();
});
});
// Piece 2. Changes constantly. OK as is
postJSON('@Url.Action("GetNotifications", "NotificationAsync")', function …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Internet Explorer中实现缺少CORS功能的解决方法.对于GET请求,我使用JSONP,这里没问题.对于小的POST/DELETE/PUT请求,我也通过GET隧道化请求来使用JSONP,但这对较大的请求不起作用(因为GET URL的长度是有限的).因此,对于大数据,我尝试通过iframe实现表单POST.由于同源策略,我无法读取此POST的响应,因此我在发布数据后通过JSONP GET请求获取响应.效果很好但有时我在IE 9中收到一个奇怪的警告:
Internet Explorer has modified this page to help prevent cross-site scripting.
Run Code Online (Sandbox Code Playgroud)
首先,我想知道IE在那里做了什么,因为即使出现此警告,一切仍然可以正常工作.然后我发现IE在POST回答(我无法阅读并且无论如何需要)之后用"#"字符替换隐藏的iframe的内容.
因此,即使出现此警告,我的解决方法仍然有效,但我想知道究竟是什么触发了这个警告,所以也许我可以修改我的CORS解决方法来摆脱这个警告.任何提示?
诺比在这里.我正在编写一个需要从另一个域读取XML文件的客户端脚本.我尝试过使用JSONP.我得到200响应但客户端由于某种原因无法访问返回的数据.我收到两个错误:
Resource interpreted as Script but transferred with MIME type text/xml
Run Code Online (Sandbox Code Playgroud)
和
Uncaught SyntaxError: Unexpected token <
Run Code Online (Sandbox Code Playgroud)
这是代码(我删除了XML url,因为它是保密的):
$(document).ready(function() {
$.getJSON("urlOfFilecallback=?", function(data) {
console.log(data)
})
});
Run Code Online (Sandbox Code Playgroud)
当我尝试在控制台中呈现数据时,我得到:
ReferenceError: data is not defined
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?我需要使用代理吗?
我正在尝试使用AngularJS调用Yelp API,但我遇到了麻烦.我一直收到400坏请求,我不知道为什么.
Yelp API文档:
http://www.yelp.com/developers/documentation/v2/authentication http://www.yelp.com/developers/documentation/v2/search_api
包含Yelp API生成密钥的页面:
http://gyazo.com/fa918329eb0cde18a5db242d1d0b0b0e
这是我的代码执行调用的片段:
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
return result;
}
app.factory("MyYelpAPI", function($http) {
return {
"retrieveYelp": function(name, callback) {
$http.jsonp("http://api.yelp.com/v2/search?term=food&location=San+Francisco&callback=JSON_CALLBACK",
{
params: {
oauth_consumer_key: /* Consumer Key */,
oauth_token: /* Token */,
oauth_signature_method: "hmac-sha1",
oauth_signature: /* Token Secret */,
oauth_timestamp: new Date().getTime(),
oauth_nonce: randomString(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
}
}
).success(callback);
}
}
});
Run Code Online (Sandbox Code Playgroud) 我有以下代码,我无法理解它为什么不返回并在HTML正文中打印它.
var pageURL = document.URL;
var tweet = "https://cdn.api.twitter.com/1/urls/count.json?url='"+ pageURL + "'";
$.getJSON(tweet,function(json){
$('#twitterfeed').html(json.count);
});
<div id="twitterfeed"></div>
Run Code Online (Sandbox Code Playgroud)
https://cdn.api.twitter.com/1/urls/count.json?url=http://www.google.com
返回{"count":23844636,"url":"http://www.google.com/"}
以下似乎没有用,有没有人知道为什么?
我认为没有CORS问题。
为什么Webpack使用jsonp获取块脚本?
这是生成的webpackBootstrap。
/******/ // install a JSONP callback for chunk loading
/******/ var parentJsonpFunction = window["webpackJsonp"];
/******/ window["webpackJsonp"] = function webpackJsonpCallback(chunkIds, moreModules) {
/******/ // add "moreModules" to the modules object,
/******/ // then flag all "chunkIds" as loaded and fire callback
/******/ var moduleId, chunkId, i = 0, callbacks = [];
/******/ for(;i < chunkIds.length; i++) {
/******/ chunkId = chunkIds[i];
/******/ if(installedChunks[chunkId])
/******/ callbacks.push.apply(callbacks, installedChunks[chunkId]);
/******/ installedChunks[chunkId] = 0;
/******/ }
/******/ for(moduleId in moreModules) {
/******/ …Run Code Online (Sandbox Code Playgroud)