Jquery 跨域进行ajax调用

Jquery 跨域进行ajax调用,jquery,ajax,cross-domain,Jquery,Ajax,Cross Domain,我有一个ajax调用,我想成为跨域的,我怎么能做到这一点?脚本如下 $.ajax({ type: "GET", url: url, data: {sendername: sendername, email: email, subject: subject, message: message}, dataType: "jsonp", crossDomain: "true", success: funct

我有一个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') {
                // 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
            }
        }
    });
url返回以下
echo json\u encode($result)
如果成功,
$result
的值可以是success,如果不成功,则可以是success


PHP以以下内容结尾:“(“.json\u encode($result)”;”

只有当所访问的web服务设置为跨域访问时,才能请求并获取jsonp,因此ajax调用必须正确,web服务必须正确

ajax调用

            $.ajax({
            type: "GET",
            cache: false,
            dataType: 'jsonp',
            // we are setting base_url at the top, like http://www.MyDomain.com/MyPage.svc/
            url: base_url + "GetGfeQuote?strJsonRequestObject=" + JSON.stringify(LoadedGetQuoteObject()),
            contentType: "text/plain",
            success: function (theJson) {
                // I make sure I got json
                if (theJson.indexOf('{') > -1 ) {
                    glb_the_quote = $.parseJSON(theJson);
                    if (glb_the_quote.errorMessage.length == 0) {
                        PopulateResultsPage();                            
                    } else {
                        alert('There was an error getting the quote: ' + glb_the_quote.errorMessage);
                    }
                } else {
                    alert(theJson)
                }
            },
            error: function (req, status, error) {
                if(status == "timeout"){
                    ShowNoInternetConnectionWarning();
                } else {
                    alert("There was an internet error = " + status + ", " + error);
                }
            },
            // this gives the webservice 7 seconds to return
            timeout: 7000
        });
        // end ajax;
现在是webservice:在某一点上,我似乎必须在与webservice代码相同的目录中正确配置web配置,即.svc文件,所以我就是这么做的

以下是我在svc文件中的全部内容:

<%@ ServiceHost Language="C#" Factory="System.ServiceModel.Activation.WebServiceHostFactory"  Debug="true" Service="gfeWebService.ws.wsGfe" CodeBehind="wsGfe.svc.cs" %>

webconfig必须具有以下内容(注意crossDomainScriptAccessEnabled=“true”)


提示

  • 在js代码中的url:行附近放置一个断点,获取url:中结束的值。。。换句话说,抓住这是如何解决的

    base_url+“getgfekote?strJsonRequestObject=“+JSON.stringify(LoadedGetQuoteObject())

并将其粘贴到浏览器的地址框中。这样会得到更有意义的错误消息

  • 让小提琴手运行,而你的工作,并检查什么是发送和接收

HTH

您可以使用
YQL
绕过COR,只要您只执行get请求,而不使用会话或任何棘手的操作

    $.getJSON("http://query.yahooapis.com/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) {
            // ...
        }
    );

如果主机上没有设置CORS头,则设置
crossDomain:true
将无法神奇地工作。这应该是自动检测到的。@AndrewMao ok的可能重复我不确定CORS头是什么,但我有许多其他跨域工作的脚本,我能看到的唯一区别是有一个数据类型a回调和jsonpdatatype@Nix它给出了部分答案,但即使有改动也不起作用。请发表你的看法抱歉,我不想在php方面使用API。我的php脚本中是否需要包含任何内容?我发现我没有办法真正“获取”发送到服务的数据。然而,在firebug中,我甚至还没有深入到Web服务
    $.getJSON("http://query.yahooapis.com/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) {
            // ...
        }
    );