有点棘手的问题.
我正在开展一个项目,当用户在自助服务终端上查看我们的网站时,我们需要允许收据打印输出.由于与驱动程序和格式相关的原因,我使用Word自动化COM来处理打印收据.我已将此代码包装在本地计算机上运行的Web服务中.
计划是在页面html中将一个简单的jQuery ajax调用放到运行Web服务的本地机器的url中.此ajax调用包含订单的json对象,该对象由Web服务反序列化并打印出来.如果我使用localhost就可以正常工作,但是在生产中我会遇到没有跨域的ajax调用规则.
代理将无法工作,因为网站上运行的代码无法联系运行打印服务的本地Web服务.在浏览网页后,我发现使用JSONP可能是解决方案,但我无法弄清楚如何使其工作.大多数教程都假设您尝试获取一些远程数据而不仅仅是发布数据.打印Web服务返回void.
如何配置我的Web服务(asmx)以使用JSONP以及我的jQuery代码是什么样的?目前它看起来像这样:
function printReceipt(data) {
$.ajax({
type: "POST",
url: "http://192.9.200.165/ContestWebService/Service1.asmx/PrintOrderReceiptJson",
data: data,
dataType: "json",
contentType: "application/json; charset=utf-8",
error: function(xhr, msg) { alert(xhr.statusText); }
});
}
Run Code Online (Sandbox Code Playgroud)
任何更简单的JSONP替代方案,或者我可能没有考虑的任何其他可能的解决方案也会有所帮助.
我有一个表单输出一行简单的HTML <a href="link">A LINK</a>
我可以使用附加到网址的数据直接访问该流程,例如http://site.com/form.asp?sample=100
因为这是跨域(到子域),我试图使用JSONP.我首先尝试使用数据类型json,但我仍然得到403禁止.这是我正在尝试使用JSONP,但这是各种混乱,并返回错误与此%5Bobject%20Object%5D"附加到它.我想这是一个问题我如何尝试将数据附加到网址?
$j.getJSON({
type: 'POST',
url: 'http://site.com/form.asp',
data: 'order=' + ordervalue,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function(response) {
alert(response);
}
});
Run Code Online (Sandbox Code Playgroud) 我在Azure blob存储中有一些数据.数据是JSON,并且已使用"application/json"内容类型保存.
我的应用程序将托管在"myapp.com",这是一个包含CNAME到"myapp.cloudapp.net"的域名.我想我应该创建一个像"storage.myapp.com"这样的自定义域名,这个域名可以放到我的Azure存储中.
但是之后?我可以使用JSONP或其他方式对Azure存储进行JSON ajax调用吗?
怎么会有更好的方法呢?
非常感谢.
我需要管理像'我的JSONP请求中的char ,通过jquery进行Ajax.所以(来自C#)这就是我所做的:
myText = "Hello I'm a string";
myText.Replace("'", "\'");
Response.Write(Request["callback"] + "({ htmlModulo: '" + myText + "'});");
Run Code Online (Sandbox Code Playgroud)
但在客户端,它破了:
parsererror - SyntaxError: missing } after property list
Run Code Online (Sandbox Code Playgroud)
那么,'如果替换不起作用,我该如何管理呢?
在我们的网站中,有些页面是SSL,有些是非SSL.
例如:
http://www.example.com/search/patients
https://www.example.com/patients
Run Code Online (Sandbox Code Playgroud)
现在我在http://www.example.com/search/patients页面上搜索病人并https://www.example.com/patients通过jQuery $.ajax功能发送服务器请求dataType=json.我无法获取数据.
问题:
我应该使用jsonp,当我们从申请http到https或https到http同一台服务器上?
如果我对两个URL使用SSL,那么它dataType=json只能用于
谢谢
我在控制器中有一个方法,它将根据要求返回HTML或JSON.这是一个这种方法的精简示例,模仿我在此问题中找到的有关如何执行此操作的信息:
@RequestMapping(value="callback")
public ModelAndView callback(@RequestParam("c") String c) {
Map response = new HashMap<String, String>();
response.put("foo", "bar");
return new ModelAndView("fake", "data", new JSONPObject(c, response));
}
Run Code Online (Sandbox Code Playgroud)
我将JSONPObject放入模型中,因为我必须能够从呈现是否请求HTML的视图中访问它.但是当我使用回调渲染JSON时,这会产生一个问题:
curl 'http://localhost:8080/notes/callback.json?c=call'
{"data"call(:{"foo":"bar"})}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,因为我将数据放在模型的"数据"插槽中,当模型呈现为JSON时,会有额外的包装.我正在寻找的是渲染的JSON(技术上是JSONP)看起来像这样:
call({"data":{"foo":"bar"}})
Run Code Online (Sandbox Code Playgroud)
任何人都可以看到一种方法来获得我想要去的地方而不会破坏在渲染过程中返回访问JSONPObject的视图的能力吗?
我目前有一个WCF数据服务www.mywebsite.com.这是一个基本服务,如下所示:
namespace MyWeb
{
[JSONPSupportBehavior]
public class MyDataService : DataService<MyEntities>
{
public static void InitializeService(IDataServiceConfiguration config)
{
config.UseVerboseErrors = true;
config.SetEntitySetAccessRule("Entities", EntitySetRights.AllRead);
ServiceOperationRights.All);
}
}
}
Run Code Online (Sandbox Code Playgroud)
目前,我们有野外客户通过发布ajax调用来发出请求,例如:
$.ajax({
url: serverAddress + "MyDataService.svc/Entities?$top=20&$filter=IsConfirmed%20eq%20null&$format=json&$callback=?",
headers: {
version: "1.0",
platform: "a platform"
},
timeout: 12000,
dataType: 'jsonp',
cache: false,
context: document.body
})
Run Code Online (Sandbox Code Playgroud)
这按预期工作,返回包含Entities表中所需对象的javascript对象.
但是,我们希望在服务器端添加一些智能,以限制从此查询返回的结果.为此,我尝试在上述MyDataService类中实现Query Interceptor :
[QueryInterceptor("Entities")]
public IQueryable<Entity> OnQueryFares(IQueryable<Entity> query)
{
return from e in query
where DataCheck(e)
select e;
}
Run Code Online (Sandbox Code Playgroud)
目标逻辑是服务现在只返回DataCheck(e)评估为true的表条目.此功能似乎有效.但是,在使用客户端进行测试时,我收到以下错误:
Web Console(4448): Uncaught SyntaxError: …Run Code Online (Sandbox Code Playgroud) 我正在尝试从Symfony2返回JSONP.我可以很好地返回常规的JSON响应,但好像JSON响应类忽略了我的回调.
$.ajax({
type: 'GET',
url: url,
async: true,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(data)
{
console.log(data);
},
error: function()
{
console.log('failed');
}
});
Run Code Online (Sandbox Code Playgroud)
然后在我的控制器中:
$callback = $request->get('callback');
$response = new JsonResponse($result, 200, array(), $callback);
return $response;
Run Code Online (Sandbox Code Playgroud)
我从中获得的响应始终是常规JSON.没有回调包装.
Json Response类在这里:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/JsonResponse.php
我正在尝试使用geonames数据填充select2元素.我定义了一个formatSelection方法,但是当选择一个项目时它不会触发.
这是HTML元素:
<input id="location" size="30" type="text">?
Run Code Online (Sandbox Code Playgroud)
使用格式函数选择2绑定:
function locationFormatResult(location) {
var markup = "<table class='location-result'><tr>";
if (location.countryCode !== undefined) {
markup += "<td class='flag-image'><img src='http://www.geonames.org/flags/x/" + location.countryCode.toLowerCase() + ".gif' /></td>";
}
markup += "<td class='location-info'>";
markup += "<div class='location-name'>" + location.name + ", " + location.adminName1 + ", " + location.countryName + "</div>";
markup += "</td></tr></table>";
return markup;
}
function locationFormatSelection(location) {
return location.name + ", " + location.adminName1 + ", " + location.countryName;
}
$(function () {
$('#location').select2({
placeholder: …Run Code Online (Sandbox Code Playgroud) 在我解释我的问题之前,我想提一下,我很天真jsonp.这实际上是我第一次尝试合作JSONP.
我使用jquery ajax调用从网站上撤回数据.
我的jquery代码如下
$.fn.checkTPS = function(){
return this.each(function(){
var interval;
$(this).on('keyup', function() {
var api_key = 'asdfasfsadfsadfsad';
var format = 'json';
var username = 'dame@example.co.uk';
var self = $(this);
var selfValue;
var feedback = $('.tps-feedback');
if(interval === undefined){
interval = setInterval(function(){
if(selfValue !== self.val()) {
selfValue = self.val();
if (selfValue.length > 9){
$.ajax({
url: 'https://www.selectabase.co.uk/api/v1/tps/' + selfValue + '/',
type: 'get',
dataType: 'jsonp',
data: {
format: format,
username: username,
api_key: api_key
},
success: function(data) { …Run Code Online (Sandbox Code Playgroud)