I m trying to learn how to GET data from my rails 3.2.2 project asynchronously. I am new to both javascript and jquery but I ve learned, javascript cannot GET data from a server in a different domain. To get around that I ve read about using $.ajax(), $getJSON(), and/or $.jsonp(), so my first question is when should you use one over another?
我试图获得一种工作技术(例如,在我的铁路3.2.2项目内,Jajax()美元)。 我有一个简单的耳光,包括1个控制器 =>图像和1个实地 =>t.string:名称。
class ImagesController < ApplicationController
# GET /images
# GET /images.json
def index
@images = Image.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @images }
end
end
# GET /images/1
# GET /images/1.json
def show
@image = Image.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @image }
end
end
# GET /images/new
# GET /images/new.json
def new
@image = Image.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @image }
end
end
# GET /images/1/edit
def edit
@image = Image.find(params[:id])
end
# POST /images
# POST /images.json
def create
@image = Image.new(params[:image])
respond_to do |format|
if @image.save
format.html { redirect_to @image, notice: Image was successfully created. }
format.json { render json: @image, status: :created, location: @image }
else
format.html { render action: "new" }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# PUT /images/1
# PUT /images/1.json
def update
@image = Image.find(params[:id])
respond_to do |format|
if @image.update_attributes(params[:image])
format.html { redirect_to @image, notice: Image was successfully updated. }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.json
def destroy
@image = Image.find(params[:id])
@image.destroy
respond_to do |format|
format.html { redirect_to images_url }
format.json { head :no_content }
end
end
end
I first want to explore the index action and GET a list of image names using an $.ajax() call in up.html:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"> </script>
</head>
<body>
<script>
$(document).ready( function (){
$("button").click(function(){
$.ajax({
dataType: jsonp ,
url: "http://localhost:3000/images",
success: function(response) {
console.log(response);
}
});
});
});
</script>
<button id ="button" type ="button">Get names</button>
</body>
</html>
我收到了奥塞罗的以下资料:
event.layerX and event.layerY are broken and deprecated in WebKit. They will be removed from the engine in the near future.
jquery.min.js:16Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:3000/images? callback=jQuery15209319186636712402_1334849479698&_=1334849481034".
images:1Uncaught SyntaxError: Unexpected token <
这里损失了。 我不敢肯定,我是否应该使用? 回顾? 或者如果我在我的控制人员中有额外的参数,或者说我完全是这样吗?