jsonp

Custom headers with $.ajax type jsnop or json

荒凉一梦 提交于 2019-12-20 03:28:05
问题 I have a problem with sending some custom headers to with a jQuery ajax JSON (or JSONP) request. Code looks like this: $.ajax({ beforeSend: function(xhr) { xhr.setRequestHeader("X-VER", VER); xhr.setRequestHeader("X-TIMESTAMP", now); }, type: 'GET', data: null, url: site_uri, dataType: 'jsonp', success: function(msg){$(selector).html(msg);} }); Firebug shows no custom headers. In addition the url changes with jsonp (can I change this?) Edit: I found that it probably violates http://en

Can I avoid using global callback function when loading JSON-P?

百般思念 提交于 2019-12-20 03:16:53
问题 I want to load my Google Calendar feed on my web page using JSON-P. To make the main content on the page load quicker, I inject the script tag for JSON-P into the head tag only after page is loaded. Google Calendar API returns a script with callback function, see example here. Is it possible to avoid using global function as a callback function? I would like to wrap all the necessary code for injecting the tag and parsing the results inside one wrapper function. 回答1: No. When you are using

Returning JSONP instead of JSON from a JSP

自作多情 提交于 2019-12-20 02:53:00
问题 I found this question on setting the response type to json from a jsp but I'm in need of setting the response type to jsonp for cross-domain access. Would it still be this: response.setContentType("application/javascript"); and just wrapping the response from the jsp in callbackfunction( + content + ) or is there something more that needs to be done? 回答1: I recently had to do this. In the server side I had something like so: string callbackName = queryMap['callback']; //jquery will pass in

详解js跨域问题

大憨熊 提交于 2019-12-20 01:08:22
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 什么是跨域? 概念:只要协议、域名、端口有任何一个不同,都被当作是不同的域。 URL 说明 是否允许通信 http://www.a.com/a.jshttp://www.a.com/b.js 同一域名下 允许http://www.a.com/lab/a.jshttp://www.a.com/script/b.js 同一域名下不同文件夹 允许http://www.a.com:8000/a.jshttp://www.a.com/b.js 同一域名,不同端口 不允许http://www.a.com/a.jshttps://www.a.com/b.js 同一域名,不同协议 不允许http://www.a.com/a.jshttp://70.32.92.74/b.js 域名和域名对应ip 不允许http://www.a.com/a.jshttp://script.a.com/b.js 主域相同,子域不同 不允许http://www.a.com/a.jshttp://a.com/b.js 同一域名,不同二级域名(同上) 不允许(cookie这种情况下也不允许访问)http://www.cnblogs.com/a.jshttp://www.a.com/b.js 不同域名 不允许 对于端口和协议的不同,只能通过后台来解决。

通过jsonp解决浏览器跨域问题

删除回忆录丶 提交于 2019-12-20 01:00:51
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 一、什么是jsonp 为了便于客户端使用数据,逐渐形成了一种非正式传输协议,人们把它称作JSONP,该协议的一个要点就是允许用户传递一个callback参数给服务端,然后服务端返回数据时会将这个callback参数作为函数名来包裹住JSON数据,这样客户端就可以随意定制自己的函数来自动处理返回数据了。 二、例子 1、客户端写法 这里借用了前端jquery框架对jsonp的支持 var ajaxUrl = "http://192.168.8.141:9092/project/rest/team/matchResult/1/20/1/1000"; function localHandler(data) { console.log("fengshu") console.log(data); } var ajaxParam = { async: false, url: ajaxUrl, type: "GET", dataType: 'jsonp',//非正式跨域传输协议 jsonp: 'localHandler', success: function (json) { //回调数据在localHandler处理 } }; $.ajax(ajaxParam); 2、服务器端写法 @RequestMapping("

“jquery.jsonp.js” GET works. What about POST PUT DELETE OPTIONS?

杀马特。学长 韩版系。学妹 提交于 2019-12-19 11:52:43
问题 jsonp http methods besides GET (POST, PUT, OPTIONS, DELETE) Using jquery built-in $.ajax method looks like this $(document).ready(function() { $.ajax({ type: "GET", url: "http://myurl.com/webservice&callback=?", ... }); Only want to draw attention to the line type: "GET", With $.ajax performing a http PUT would be simply change type: "PUT", This code example comes from JSON parsing from cross domain using jquery ajax Not using $.ajax Using google-code's jquery.jsonp https://github.com

cross domain call with jQuery jsonp to ASP.NET web service

浪尽此生 提交于 2019-12-19 03:24:32
问题 My problem is known issue and discussed here and here. But even after reading and implementing the suggested solutions i am unable to make this work. The problem : the web service returning xml insted of json: <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">"Now i am getting jsop string""2nd param"</string> Now lets break the code into sections: THE REMOTE SERVER (IIS 7.0, .NET 4): web.config: <?xml version="1.0"?> <configuration> <system.webServer> <modules> <add

Synchronous cross sub-domain POST request with jQuery

跟風遠走 提交于 2019-12-19 03:13:46
问题 I'm trying to do a cross domain POST request and have hit a wall (or two). I can't put a proxy page on the server - so that is not an option. I have researched getJSON, which works great except that I need to POST not GET. Is it possible to do this? If it is not , can someone explain to me how getJSON works and why I cannot make a POST alternative. 回答1: You CANNOT make a cross-domain request (GET / POST / etc.) with an XMLHttpRequest (aka AJAX). What you can do, when the server supports it,

原生 JavaScript 实现 AJAX、JSONP

雨燕双飞 提交于 2019-12-19 01:17:44
相信大多数前端开发者在需要与后端进行数据交互时,为了方便快捷,都会选择 JQuery 中封装的 AJAX 方法,但是有些时候,我们只需要 JQuery 的 AJAX 请求方法,而其他的功能用到的很少,这显然是没必要的。 其实,原生 JavaScript 实现 AJAX 并不难,这篇文章将会讲解如何实现简单的 AJAX ,还有跨域请求 JSONP ! 一、AJAX AJAX的核心是 XMLHttpRequest 。 一个完整的 AJAX 请求一般包括以下步骤: 实例化 XMLHttpRequest 对象 连接服务器 发送请求 接收响应数据 我将 AJAX 请求封装成 ajax() 方法,它接受一个配置对象 params 。 function ajax(params) { params = params || {}; params.data = params.data || {}; // 判断是ajax请求还是jsonp请求 var json = params.jsonp ? jsonp(params) : json(params); // ajax请求 function json(params) { // 请求方式,默认是GET params.type = (params.type || 'GET').toUpperCase(); // 避免有特殊字符,必须格式化传输数据

jsonp跨域请求

跟風遠走 提交于 2019-12-18 23:24:13
1.什么是跨域   跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器施加的安全限制。   所谓同源是指,域名,协议,端口均相同。 2.什么是jsonp   Jsonp其实就是一个跨域解决方案。Js跨域请求数据是不可以的,但是js跨域请求js脚本是可以的。可以把数据封装成一个js语句,做一个方法的调用。跨域请求js脚本可以得到此脚本。得到js脚本之后会立即执行。可以把数据做为参数传递到方法中。就可以获得数据。从而解决跨域问题。 3. jsonp的原理   浏览器在js请求中,是允许通过script标签的src跨域请求,可以在请求的结果中添加回调方法名,在请求页面中定义方法,既可获取到跨域请求的数据。 4.服务端代码 方式一:自己拼装返回结果 @ResponseBody public String getItemCatList(String callback) { //此处获取对象信息 CatResult catResult = itemCatService.getItemCatList(); //把pojo转换成json字符串 String json = JsonUtils.objectToJson(catResult); //拼装返回值//用回调函数名称包裹返回数据,这样,返回数据就作为回调函数的参数传回去了 String result =