0

クロスドメインになりたい 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_encode($result);の値を返します$result。成功した場合は成功し、失敗した場合はそれ以外の可能性があります。

PHPはこれで終わりますecho $_GET['callback']."(".json_encode($result).");";

4

2 に答える 2

0

ヒットしている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;

さて、Webサービス:ある時点で、Webサービスコードと同じディレクトリ(.svcファイル)にWeb構成を正しく構成する必要があるように見えたので、それが私がしていることです。

svc ファイルに入れるのはこれだけです。

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

また、webconfig には次のようなものが必要です ( crossDomainScriptAccessEnabled="true" に注意してください)。

<system.serviceModel>   
            <behaviors>
                    <endpointBehaviors>
                        <behavior name="webHttpBehavior">
                            <webHttp />
                        </behavior>
                    </endpointBehaviors>
                </behaviors>

                <bindings>
                    <webHttpBinding>
                        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
                    </webHttpBinding>
                </bindings>

                <!-- the names have to be fully qualified. If you get an error that says, I can't find blah blah, you don't have the names right -->
                <services>
                    <service name="gfeWebService.ws.wsGfe"> 
                        <endpoint  address=""
                                   binding="webHttpBinding"
                                   bindingConfiguration="webHttpBindingWithJsonP"
                                   contract="gfeWebService.ws.IwsGfe"
                                   behaviorConfiguration="webHttpBehavior"
                        >
                        </endpoint>
                    </service>
                </services>

</system.serviceModel>

チップ

  • js コードの url: 行の近くにブレーク ポイントを配置し、url: で終わる値を取得します。つまり、これがどのように解決されるかを取得します。

    base_url + "GetGfeQuote?strJsonRequestObject=" + JSON.stringify(LoadedGetQuoteObject())

ブラウザのアドレスボックスに貼り付けます。そうすれば、より意味のあるエラー メッセージが表示されます。

  • これに取り組んでいる間は Fiddler を実行し、何が送信され、何が受信されているかを確認してください。

HTH

于 2013-03-26T03:02:29.610 に答える
0

YQLGET リクエストのみを実行し、セッションやトリッキーなものを使用しない限り、CORS を回避するために使用できます。

    $.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) {
            // ...
        }
    );
于 2013-03-26T03:30:32.330 に答える