2013-03-26 89 views
0

我有一個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') { 
       // 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).");";

+0

如果在主機上沒有設置CORS頭,設置'crossDomain:true'將不會奇蹟般地工作。無論如何,這應該是自動檢測的。 – 2013-03-26 02:01:12

+0

[jQuery ajax跨域]的可能重複(http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) – Nix 2013-03-26 02:02:45

+1

@AndrewMao確定不知道CORS頭是什麼,但我有許多其他腳本工作跨域,唯一的區別是我可以看到的是,有一個數據類型的回調和jsonp數據類型 – 2013-03-26 02:05:21

回答

0

您可以申請並獲得JSONP只有當你打的Web服務建立跨域訪問,讓您的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配置,正確配置,在同一個目錄的web服務代碼 - .svc文件 - 使是我所做的。

這是我把我的SVC文件:

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

而且webconfig必須具有的東西,如以下(注意crossDomainScriptAccessEnabled = 「真」)

<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:line,抓取結束於url的值:。 ..換句話說,抓住這如何解決

    BASE_URL + 「GetGfeQuote?strJsonRequestObject =」 + JSON.stringify(LoadedGetQuoteObject())

,並粘貼在瀏覽器的地址欄中。你會以這種方式得到更有意義的錯誤消息。

  • 在您處理此操作時,Fiddler正在運行,並檢查收到的正在發送的內容。

HTH

+0

的OP返回要做的就是使用PHP – 2013-03-26 03:27:48

0

您可以使用YQL繞過CORS,只要你只做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) { 
      // ... 
     } 
    ); 
+0

對不起,我不想使用該API – 2013-03-26 11:16:29

+0

中的條款PHP有什麼我需要包括在我的PHP腳本我看到我沒有一種實際「獲取」發送到服務的數據的方式。然而,在螢火蟲甚至沒有達到那麼遠,以達到網絡服務 – 2013-03-26 11:18:11