使ajax调用跨域

编程入门 行业动态 更新时间:2024-10-11 19:23:36
本文介绍了使ajax调用跨域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个ajax调用我想成为跨域如何我能这样做?脚本在下面

$。ajax({ type:GET, url:url , data:{sendername:sendername,email:email,subject:subject,message:message}, dataType:jsonp, crossDomain:true, success :function(data){ if(data =='success'){ //显示谢谢你记得在 $里添加一个对话框contactpage.find('。contact-thankyou' ).show(); $ contactpage.find('。contact-form')。hide(); } else { alert('无法发送您的信息,请重试。'); //记得在}中添加一个对话框} } });

url返回以下 echo json_encode($ result); $ result 的值可以是成功的,如果成功则为其他任何值。

PHP以此 echo $ _GET ['callback']。(。json_encode($ result)。);;

class =h2_lin>解决方案

您可以使用 YQL 绕过CORS,只要您只执行GET请求,使用会话或任何棘手的问题。

$ .getJSON(query.yahooapis/v1/public/yql ?+ q = select%20 *%20from%20html%20 where%20url%3D%22+ encodeURIComponent(base_url +GetGfeQuote?strJsonRequestObject =+ JSON.stringify(LoadedGetQuoteObject b $ b%22& format = xml'& callback =?, function(theJson){ // ... } );

I have an ajax call I would like to become cross domain how can I do this? The script is below

$.ajax({ type: "GET", url: url, data: {sendername: sendername, email: email, subject: subject, message: message}, dataType: "jsonp", crossDomain: "true", success: function (data) { if (data == 'success') { // show thank you remember to add a dialog inside $contactpage.find('.contact-thankyou').show(); $contactpage.find('.contact-form').hide(); } else { alert('Unable to send your message. Please try again.'); //remember to add a dialog inside } } });

The url returns the following echo json_encode($result); the value of $result can be success if successful and anything else if unsuccessful.

the PHP ends with this echo $_GET['callback']."(".json_encode($result).");";

解决方案

You can use YQL to get around CORS, as long as you're only doing GET requests, and not using sessions or anything tricky.

$.getJSON("query.yahooapis/v1/public/yql?" + "q=select%20*%20from%20html%20where%20url%3D%22" + encodeURIComponent( base_url + "GetGfeQuote?strJsonRequestObject=" + JSON.stringify(LoadedGetQuoteObject())) + "%22&format=xml'&callback=?", function (theJson) { // ... } );

更多推荐

使ajax调用跨域

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

发布评论

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

>www.elefans.com

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