2013-05-30 33 views
-1

编辑 - 已解决。向其他服务器发送ajax请求(具体示例不起作用)

谢谢大家花时间回复。经过数小时的研究,我被引导到CORS,但事实证明我应该一直在看JSONP。我读了一些教程,我想我明白了。再次感谢。

我想要做的是将用户输入从表单传递到我的服务器,这与表单所在的服务器不同。我不会花太长的时间阅读,所以我会直接跳到代码。

在下面的JavaScript代码中,我通过tagName获取了安全问题的元素,我不想在我的html表单中使用名称属性。然后,我将检索到的数据存储到JSON数据类型中,然后我将其称为ajax函数。

function processForm() { 
     var i, userInput, inputType; 
     var name, creditNo, cvv, month, year; 
     var inputs = document.getElementsByTagName("input"); 
     for (i=0; i < inputs.length; i++){ 
      inputType = inputs[i].getAttribute('data-tajer'); 
      userInput = inputs[i].value; 
      if (inputType == 'name') { 
       name = userInput; 
      } 
      else if (inputType == 'cc') { 
       creditNo = userInput; 
      } 
      else if (inputType == 'cvv') { 
       cvv = userInput; 
      } 
      else if (inputType == 'month') { 
       month = userInput; 
      } 
      // year 
      else { 
       year = userInput 
      } 
     } 
     var myJASON = {"name": name, "cc": creditNo, "cvv": cvv, "month": month, "year": year}; 
     var strJSON = JSON.stringify(myJASON); 
     ajaxCall(strJSON); 
    } 

现在的ajax调用,这应该是微不足道的。这里唯一的区别是我的网址位于不同的服务器上。

function ajaxCall(param){ 

     var url = 'http://blahbalh'; 

     // jquery code snippet 
     var ajaxRequest; 

     try{ 
      ajaxRequest = new XMLHttpRequest(); 
     } catch (e){ 
      try{ 
       ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) { 
       try{ 
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
       } catch (e){ 
        alert("Please update your browser biatch!"); 
        return false; 
       } 
      } 
     } 
     // Create a function that will receive data sent from the server 
     ajaxRequest.onreadystatechange = function(){ 
      if(ajaxRequest.readyState == 4){ 
       var ajaxDisplay = document.getElementById('ajaxDiv'); 
       ajaxDisplay.innerHTML = ajaxRequest.responseText; 
      } 
      else { 
       alert("failed"); 
      } 
     } 
     ajaxRequest.open("POST", url, true); 
        // param is the JSON data. 
     ajaxRequest.send(param); 
    } 

基本上现在发生的事情是,我得到的0回状态和1 readyState的你们能发现什么,我做错了什么?请记住,我首先在jQuery中使用它,但它也不起作用。我接受任何解决方案和建议。

为了方便,我将提供html表单。

<form id="paymentForm"> 
     <h3>Payment Form</h3> 
     <h4>Please fill in the form below, * are required fields.</h4> 
     <div> 
      <label> 
       <span>Name: *</span> 
       <input placeholder="Please enter your name as it appears on your credit card." id = "name" type="text" data-tajer="name" tabindex="1" required autofocus> 
      </label> 
     </div> 
     <div> 
      <label> 
       <span>Credit Card: *</span> 
       <input placeholder="Please enter credit card number" type="text" data-tajer="cc" tabindex="2" required> 
      </label> 
     </div> 
     <div> 
      <label> 
       <span>CVV: *</span> 
       <input placeholder="Please enter CVV code found on the back of your card" type="text" data-tajer="cvv" tabindex="3" required> 
      </label> 
     </div> 
     <div> 
      <label> 
       <span>Expiry Month: *</span> 
       <input placeholder="Please enter the expiry month of your credit card" type="text" data-tajer="month" tabindex="4" required> 
      </label> 
     </div> 
     <div> 
      <label> 
       <span>Expiry Year: *</span> 
       <input placeholder="Please enter expiry year of your credit card" type="text" data-tajer="year" tabindex="5" required></input> 
      </label> 
     </div> 
     <div> 
      <button onclick="processForm()">Process Payment</button> 
     </div> 
    </form> 
+0

这可能是从http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain重复。检查,看看是否有答案,满足您的需求:) – jvilhena

+0

如果你有机会到远程服务器,你不需要它工作在旧的浏览器,它能够更好地使用CORS – igor

回答

3

跨源访问是你不能这样做,除非你让问题域名将被列入白名单或使用JSONP

假设您位于域名abc.com,并且您想向域名xyz.com发出请求。要做到这一点,你需要跨越领域的界限,在大多数浏览器领域都是不允许的。

绕过此限制的一项是标签。当您使用脚本标记时,域限制将被忽略,但在正常情况下,您无法对结果做任何事情,只会对脚本进行评估。

输入JSONP。当您向启用了JSONP的服务器发出请求时,您会传递一个特殊参数,告诉服务器关于您的页面的一些信息。这样,服务器就可以很好地将其响应以您的页面可以处理的方式包装起来。

jsonp实际上是一个简单的技巧来克服XMLHttpRequest相同的域策略。 (如你所知不能发送ajax(XMLHttpRequest)请求到不同的域)。

所以 - 而不是使用XMLHttpRequest,我们必须使用脚本html标签,你通常用来加载js文件,为了js从另一个域获取数据。听起来怪怪的?

事情是 - 原来的脚本标签可以以类似于XMLHttpRequest的方式使用!

+0

欢呼花花公子,真棒答案。 我将重写我的代码回到jQuery,因为我读它适合与JSONP。 – zsawaf

+0

请提供你从哪里得到它的来源像http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about否则让你的贡献 –