我有一个返回JSON的Web服务,但现在我想修改它以允许调用者指定一个回调函数,所以返回从:JSON DATA到specifiedFunction(JSON DATA); 我现在返回JSON的方式就是返回一个对象的实例并让.NET执行其序列化魔术,如果我改为只返回一个字符串我可以添加函数的名称和数据的括号但是然后我在回报中最终得到引号,因为它是一个字符串,我不想要那些.那我该怎么办呢?
原因是我们希望开发人员调用我们的API能够使用动态脚本标记,如http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag所述. HTML
我注意到facebook graph api为所有方法返回的JSONP输出中有一个空注释块.
我打过的网址:
https://graph.facebook.com/NUMERIC_FACEBOOK_ID/friends?access_token=ACCESS_TOKEN_STRING&callback=theGreatFunction
Run Code Online (Sandbox Code Playgroud)
JSONP输出是:
/**/ theGreatFunction({
"data": [
{
"name": "First Friend",
"id": "XXXX"
},
{
"name": "Second Friend",
"id": "XXXXXX"
},
........
Run Code Online (Sandbox Code Playgroud)
我的问题是:/* */回调函数前的空注释块表示什么?它有特殊的目的吗?它修复了任何已知的javascript问题吗?
我的问题是已知的问题,在这里和这里讨论.但即使在阅读并实施建议的解决方案后,我也无法完成这项工作.
问题:Web服务返回xml的xml:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">"Now i am getting jsop string""2nd param"</string>
Run Code Online (Sandbox Code Playgroud)
现在让我们将代码分成几部分:
远程服务器(IIS 7.0,.NET 4):
web.config:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<modules>
<add name="JsonHttpModule.JsonHttpModule" type="JsonHttpModule"/>
</modules>
</system.webServer>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="102400"/>
</webServices>
</scripting>
</system.web.extensions>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off"/>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
网络服务:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using JsonHttpModule;
/// <summary>
/// Summary description for JSONP_EndPoint …Run Code Online (Sandbox Code Playgroud) 我正在使用jsonp和ajax访问另一台服务器上的Web服务.这是jQuery:
$.ajax({
type: 'GET',
url: wsurl + 'callback=?',
dataType: 'jsonp',
crossDomain: true,
error: function(data) {
console.log('error', data);
},
success: function(data) {
console.log('success', data);
},
complete: function() {
console.log('done');
}
});
Run Code Online (Sandbox Code Playgroud)
问题是正在调用错误回调.它给了我这个非常有用的信息:
{
readyState: 4,
status: 200,
statusText: "success"
}
Run Code Online (Sandbox Code Playgroud)
这是我正在调用的json文件:
{
"id": 0,
"room_number": "0",
"first_name": "Admin",
"last_name": "Istrator",
"password": "",
"salutation": "Mr.",
"telephone": "",
"email": "",
"description": "admin",
"checkin_date": 915797106000,
"checkout_date": 4071557106000,
"last_login_date": 947333106000,
"active_status": true,
"created_date": 915797106000,
"created_by": 0,
"reference_id": ""
}
Run Code Online (Sandbox Code Playgroud)
我首先尝试使用getJSON jQuery方法,结果相同.以为我会尝试基础ajax方法,因为它有更多的控制权,但正如你所看到的,没有运气.那么,请注意我做错了什么?知道为什么它会抛出一个错误并给我一个statusText属性的成功值吗?
好吧,我在ajax调用中添加了一些选项,并从url中删除了回调参数.这是新的ajax调用:
$.ajax({
type: …Run Code Online (Sandbox Code Playgroud) 我正在使用Recurly.js集成我的rails应用程序.
在我从我的服务器端应用程序发出重复请求之前,因此我能够将所有与优秀VCR gem(https://github.com/myronmarston/vcr)的集成存根,但Recurly.js直接向服务发出请求使用JSONP的javascript代码.
问题是:如何在集成测试中模拟这些jsonp调用?
目前我正在使用rspec + capybara + phantomjs驱动程序(https://github.com/jonleighton/poltergeist)
这是我试过的东西......
<html>
<head>
<title>bugstats.com</title>
</head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://jquery-json.googlecode.com/files/jquery.json- 1.3.min.js"></script>
<script type="text/javascript" >
function hello(){
var myObject = {"method":"User.login", /* is this the right method to call? */
"params":[ { "login" :"user", /*should i include the login credentials here? */
"password" : "pass123" ,
"remember" : "True"} ] };
var enc = $.toJSON(myObject);
$.ajax({"contentType":"application/json",
"data": enc,
"crossDomain":"true",
"dataType": "json",
"url": "https://bugzilla.company.com/bugzilla/jsonrpc.cgi", /* is this correct or should it be https://bugzilla.company.com/bugzilla/jsonrpc.cgi?method=User.login? */
"type": "POST",
success: function(){
alert("Hallelujah");
console.log(arguments);
}, …Run Code Online (Sandbox Code Playgroud) 可能重复:
Ajax跨域调用
我的应用程序使用ASP .Net Web API 4.5 RTM,它与不同域上的HTML5和AJAX交互.
此应用程序是否需要使用JSONP而不是JSON进行序列化/反序列化?
我想创建一个搜索框,返回来自不同国家/地区的Google自动建议回复.我发现了重新创建自动完成搜索的一个很好的例子:
var suggestCallBack; // global var for autocomplete jsonp
$(document).ready(function () {
$("#search").autocomplete({
source: function(request, response) {
$.getJSON("http://suggestqueries.google.com/complete/search?callback=?",
{
"hl":"en", // Language
"jsonp":"suggestCallBack", // jsonp callback function name
"q":request.term, // query term
"client":"youtube" // force youtube style response, i.e. jsonp
}
);
suggestCallBack = function (data) {
var suggestions = [];
$.each(data[1], function(key, val) {
suggestions.push({"value":val[0]});
});
suggestions.length = 5; // prune suggestions list to only 5 items
response(suggestions);
};
},
});
});
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何限制它到特定的国家,似乎没有可以传递给谷歌汽车建议的参数的文件.
如果有人有关于如何实现这一目标的建议,那将是很好的.谢谢!
我试图从雅虎的api获得股票报价,我正在使用angular的$ http.jsonp方法.目标是当结果返回时,让应用程序转到这条路线:'/ stocks/show_stock'.我试图以两种方式做到这一点并且都不起作用1)我发表声明:
window.location = '/stocks/show_stock'
Run Code Online (Sandbox Code Playgroud)
在包装JSONP响应的回调函数中2)我把语句:
$location.path '/stocks/show_stock'
Run Code Online (Sandbox Code Playgroud)
在HTTPpromise回调中.(参见代码中的注释)
这是我的代码(在coffescript中):
#THIS IS THE CALLBACK FUNCTION THAT I SEND WITH THE JSONP REQUEST
window.stock_quote_callback = (data)->
console.log data #THIS WORKS AND I CAN SEE THE DATA RETURNED FROM YAHOO
window.stock_quote_result = data.results
alert 'I am in the callback'
#THE STATEMENT BELOW DOES NOT WORK EVEN THOUGH I CAN SEE THE ALERT ABOVE
window.location = '/stocks/show_stock'
angular.module('Services').service 'StockSupplier', ($http)->
get_stock = (symbol)->
q = 'select * from yahoo.finance.quotes
where symbol in …Run Code Online (Sandbox Code Playgroud) Typeahead.js是一个非常受欢迎的Twitter自动提交库.
我刚刚安装,它似乎不支持通过JSONP的跨域请求.我得到关于远程起源的错误不允许.
我google了一下,我找不到任何相关的东西.
任何人都可以确认是否支持此功能.
jsonp ×10
javascript ×3
jquery ×3
ajax ×2
json ×2
web-services ×2
.net ×1
angularjs ×1
asp.net ×1
autocomplete ×1
bugzilla ×1
callback ×1
capybara ×1
coffeescript ×1
cross-domain ×1
html5 ×1
rspec ×1
typeahead ×1
wcf ×1