2012-07-30 116 views
0

我知道這段代碼工作正常,但是當我把這段代碼放在我的(ruby on rails)項目上時,它不起作用。

<script type="text/javascript"> 
    $.ajax({ 
      type: "POST", 
      url: "http://www.mywebsite.com", 
      data: { name: "John", location: "Boston" } 
      }).done(function(msg) { 
      alert("Data Saved: " + msg); 
     }); 
    </script> 

但是,當我更換工作$。阿賈克斯()$獲得()

$.get("https://stackoverflow.com/users/20.json", function(data) 
     { 
     }).done(function(msg) { 
      alert("Data Saved: " + msg); 
     }); 

有人知道爲什麼我的(Ruby on Rails的)項目$ .ajax不起作用,但$ .get()工作正常嗎?

+0

ajax調用是否正確地到達您的服務器? – dcpomero 2012-07-30 21:49:53

+1

定義「不起作用」。 – 2012-07-30 21:52:02

+0

另外考慮到get和post是完全不同的,我會假設你沒有POST的正確路由。試試$ .ajax({...,輸入:'GET',...}) – travis 2012-07-30 21:56:14

回答

-1

我想你是用這個jQuery。嘗試用最新版本替換你的jQuery。

1

解決的辦法是補充:

dataType: "script" 
1

是路由限制GET請求的處理?這將解釋「基於POST」的查詢(你的第一個例子)不起作用,而基於「GET」的查詢則起作用。

http://guides.rubyonrails.org/routing.html查找有關此信息...

你可以嘗試只改變你的$就功能「類型」選項是這樣的:

<script type="text/javascript"> 
$.ajax({ 
     type: "GET", 
     url: "http://www.mywebsite.com", 
     data: { name: "John", location: "Boston" } 
     }).done(function(msg) { 
     alert("Data Saved: " + msg); 
    }); 
</script> 

編輯:哎呀,看到你在我打字時修復了這個問題。 :)

+0

謝謝:) – user1560922 2012-07-30 22:18:39