I'm currently trying to make a Ajax script to communicate with an Rails server on my localhost (for now). The problem is that I specify in my $.ajax request that I want the format in 'json' but rails returns a 'html' format:
我目前正在尝试创建一个Ajax脚本,以便与本地主机上的Rails服务器通信(目前)。问题是我在$中指定了。ajax请求,我想要格式为'json',但rails返回一个'html'格式:
$(document).ready(function(){
$('form').on("submit",function(){
$.ajax({
contentType: 'application/json; charset=utf-8',
url : "http://192.168.0.36:3000/?value=10",
type : "GET",
dataType : 'JSON',
success: function(data){
alert(JSONParsedata(data));
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert(textStatus +", " +errorThrown);
}
});
return false;
})
In the console log of the rails server, I can read:
在rails服务器的控制台日志中,我可以看到:
Processing by WelcomeControllerindex as HTML ... Completed 200 OK in 34ms...
通过welcome econtrollerindex作为HTML进行处理……在34ms完成了200 OK…
I actually receive a response from the server, but in a HTML format instead of JSON.
实际上,我从服务器收到了响应,但是是HTML格式,而不是JSON。
I precise that my rails controller for welcomeController code contains the:
我精确地说,我的rails控制器的欢迎控制代码包含:
respond_to do |format|
format.html
format.json { render :json => @z }
end
z is a variable I want to send back to the Ajax request and is defined before (i lighted the code to make it understandable)
z是一个我想要发送回Ajax请求的变量,在此之前定义过(为了便于理解,我使用了代码)
As a result, Jquery is trying to parse the result in a JSON format and then finish by a : parsererror syntaxerror unexpected token '<' Which correspond to the first character of the full page!
结果,Jquery试图以JSON格式解析结果,然后以a: parsererror syntaxerror意外令牌'<'结束,该令牌对应于整个页面的第一个字符!
I searched for hours and I don't know how to resolve this.
我找了好几个小时,我不知道该怎么解决这个问题。
Thank you very much for your help
非常感谢你的帮助
4 个解决方案
#1
1
Add header
添加标题
'Accept: application/json'
#2
0
try changing { render :json => @z } to render json: { data: @z }
尝试更改{render:json => @z}以呈现json:{数据:@z}
also change "JSON" to "json"
还将“JSON”更改为“JSON”
#3
0
Actually I solved this problem this morning:
实际上我今天早上解决了这个问题:
This method worked in a basic browser such as Chrome but wasn't enough for "Rhomobile", an app IDE I'm using (even using the same Ajax request!). To solve this, I added a parameter "format" to the request and a value "json" (&format=json) and I added a condition to my controller: If the variable 'format' exists && format == 'json' then render the result to JSON. Else it renders in HTML. This works on both Rhomobile and Browser.
这种方法在Chrome等基本浏览器中有效,但对于我正在使用的应用IDE“Rhomobile”(即使使用相同的Ajax请求)来说还不够。为了解决这个问题,我向请求添加了一个参数“format”和一个值“json”(&format=json),并向我的控制器添加了一个条件:如果变量“format”存在&& format=“json”,那么将结果呈现给json。否则它会以HTML格式呈现。这对Rhomobile和Browser都有效。
I am not sure why this worked on a basic browser and not on Rhomobile but I think it's due to the 'Cross-domain' request. (I used 'rack-cors' server-side). Now it works on both.
我不确定为什么它能在基本浏览器上运行,而不是Rhomobile上,但我认为这是“跨域”请求的结果。(我使用“rack-cors”服务器端)。现在这两种方法都有效。
Thanks to all of you for your help.
谢谢大家的帮助。
#4
0
you can add a "json" postfix to your api like call http://host:port/api.json
您可以向api添加“json”后缀,如调用http://host:port/api.json
#1
1
Add header
添加标题
'Accept: application/json'
#2
0
try changing { render :json => @z } to render json: { data: @z }
尝试更改{render:json => @z}以呈现json:{数据:@z}
also change "JSON" to "json"
还将“JSON”更改为“JSON”
#3
0
Actually I solved this problem this morning:
实际上我今天早上解决了这个问题:
This method worked in a basic browser such as Chrome but wasn't enough for "Rhomobile", an app IDE I'm using (even using the same Ajax request!). To solve this, I added a parameter "format" to the request and a value "json" (&format=json) and I added a condition to my controller: If the variable 'format' exists && format == 'json' then render the result to JSON. Else it renders in HTML. This works on both Rhomobile and Browser.
这种方法在Chrome等基本浏览器中有效,但对于我正在使用的应用IDE“Rhomobile”(即使使用相同的Ajax请求)来说还不够。为了解决这个问题,我向请求添加了一个参数“format”和一个值“json”(&format=json),并向我的控制器添加了一个条件:如果变量“format”存在&& format=“json”,那么将结果呈现给json。否则它会以HTML格式呈现。这对Rhomobile和Browser都有效。
I am not sure why this worked on a basic browser and not on Rhomobile but I think it's due to the 'Cross-domain' request. (I used 'rack-cors' server-side). Now it works on both.
我不确定为什么它能在基本浏览器上运行,而不是Rhomobile上,但我认为这是“跨域”请求的结果。(我使用“rack-cors”服务器端)。现在这两种方法都有效。
Thanks to all of you for your help.
谢谢大家的帮助。
#4
0
you can add a "json" postfix to your api like call http://host:port/api.json
您可以向api添加“json”后缀,如调用http://host:port/api.json