AJAX跨网域请求

编程入门 行业动态 更新时间:2024-10-27 08:24:57
本文介绍了AJAX跨网域请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在我的主网域上有我的JavaScript档案,而我想从子网域拨打一些电话。

I have my JavaScript files on my main domain and I want to do some calls from the subdomain.

我已经添加:

url: "domain/ajax.php"

因此,完整代码是:

$.ajax({ type: "POST", url: "domain/ajax.php", data: { var1: var1, var2: var2 }, success: function(data){ } });

但是在Firebug上,它会将请求显示为红色 。

But on Firebug it shows the request as red and it fails to respond. Also the POST parameters are there as they should.

我应该在子域上创建一个新的JS文件,并添加必要的代码,然后从那里AJAX调用?

Should I create a new JS file on the subdomain and add the necessary codes and do from there the AJAX calls?

EDIT:使用JSONP代码

> localhost / ajax.php (我从 sub.localhost

I am using this on localhost/ajax.php, which I call from sub.localhost

$.ajax({ dataType: 'jsonp', data: 'id=10', jsonp: 'jsonp_callback', url: 'localhost/ajax.php', success: function (data) { console.log(data); }, });

,ajax.php包含:

and the ajax.php contains:

<?php echo $_GET["id"]; ?>

推荐答案

您可以使用

You can use Access-Control-Allow-Origin header to enable cross-domain requests.

阅读这里:跨源资源共享(CORS)

更多推荐

AJAX跨网域请求

本文发布于:2023-08-06 04:52:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310180.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AJAX   跨网域

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!