2013-03-26 130 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