ajax跨域访问jsonp,返回jquery.... is not defined

好疼啊 2016-03-16 09:17:39
这两行是火狐调试出现的错误

ReferenceError: jQuery17105408270653883338_1458090875372 is not defined
jQuery17105408270653883338_1458090875372({"name":"zhangsan"})





$.ajax({
url : url,
type:"POST",
dataType:"jsonp",
jsonp:"callbackParam",
async:false,
success:function(msg){
alert(msg+" "+"进来了");
//if(msg == "error"){
// alert(1);
//}else if(msg == "success"){
// alert(2);
// window.open(url,'NewWindow','height='+iHeight+',innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no,directories =no');
//}else{
// alert($("script",msg).html());
// alert($("script",msg).text());
//}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);//parsererror
}
});



求大神帮帮忙
...全文
1764 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
antandantABC 2017-08-31
  • 打赏
  • 举报
回复
这种方法也行: $.getJSON("http://localhost:8080/firstWeb/test?callback=?",function(data){ $(".test").text("Result:"+data.key) });
antandantABC 2017-08-31
  • 打赏
  • 举报
回复
我的这样子成功了

$.ajax({    
         type : "get",    
         async:false,    
         url : "http://localhost:8080/firstWeb/test",    
         dataType:"jsonp",//数据类型为jsonp    
         jsonp:"callback",//服务端用于接收callback调用的function名的参数    
         success:function(data){    
             $(".test").text("Result:"+data.key)    
         },    
         error:function(){
        	 $(".test").text("Result:失败");       
         }    
     });   
 

    	@ResponseBody
	@RequestMapping("/test")
	public String test(String callback) {
		Map<String,String> map=new HashMap<String,String>();
		map.put("key", "content");
		JSONObject resultJSON = JSONObject.fromObject(map); 
		String res=callback+"("+resultJSON.toString(1, 1)+")";
		System.out.println(res);
		return res;
	}
Jelar&Erza 2017-04-19
  • 打赏
  • 举报
回复
楼主是怎么解决的啊 求指教 我也遇到了这问题
  • 打赏
  • 举报
回复
引用 4 楼 LanPangBaBa 的回复:
[quote=引用 3 楼 u010425898 的回复:] ajax跨域服务端要返回callback参数,这个参数是跨域请求时带过去的

dataType:"jsonp",
                    jsonp:"callbackParam",
java后台,都有参数的呀

PrintWriter out = response.getWriter();
    	
    	JSONObject res = new JSONObject();  
    	res.put("d", "dddd");  
    	String callbackParam = request.getParameter("callbackParam");//客户端请求参数
    	out.print(callbackParam + "({\"name\":\"zhangsan\"})");//返回jsonp格式数据
    	out.flush();
    	out.close();
[/quote] callback参数和其他参数一样json格式,name:value,不要单独列出来
好疼啊 2016-03-17
  • 打赏
  • 举报
回复
引用 3 楼 u010425898 的回复:
ajax跨域服务端要返回callback参数,这个参数是跨域请求时带过去的

dataType:"jsonp",
                    jsonp:"callbackParam",
java后台,都有参数的呀

PrintWriter out = response.getWriter();
    	
    	JSONObject res = new JSONObject();  
    	res.put("d", "dddd");  
    	String callbackParam = request.getParameter("callbackParam");//客户端请求参数
    	out.print(callbackParam + "({\"name\":\"zhangsan\"})");//返回jsonp格式数据
    	out.flush();
    	out.close();
好疼啊 2016-03-16
  • 打赏
  • 举报
回复
引用 1 楼 Q80470101 的回复:
你引入了jquery?
引用了,开发的是火狐插件,页面都可以调用到jquery,调用jsonp就没用了


function doPageMonitor(config) {
	console.log("addon do page..." + JSON.stringify(config));
  for (var m in config.monitors) {
      m = config.monitors[m];
      pageMod.PageMod({
          include: m.urlPattern,
          contentScriptWhen: "ready",
          attachTo:["existing", "top", "frame"],
          contentScriptFile: [self.data.url("jquery-1.7.1.js"), self.data.url("my-script.js")],
          contentScriptOptions: {mConfig: m}
      });
  }
}
家里敷泥呀 2016-03-16
  • 打赏
  • 举报
回复
你引入了jquery?
  • 打赏
  • 举报
回复
ajax跨域服务端要返回callback参数,这个参数是跨域请求时带过去的

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧