[英]Ajax form get error on Nodejs Controller calling Jquery getJSON
I got a Node.js / Sails.js app with a client form that submit to a Controller via Jquery ajax: 我有一个带有客户端表单的Node.js / Sails.js应用程序,该表单通过Jquery ajax提交给Controller:
$(function () {
$("form").submit(function (e) {
e.preventDefault();
var form = $(this);
$.ajax({
url: form.attr("action"),
type: "POST",
dataType: 'json',
data: form.serialize(),
success: function(json){
alert( "Data Returned: " + JSON.stringify(json));
},
error: function(xhr, status, error) {
alert( "error" );
}
});
});
});
My controller ArticleController.js
try to get info from Flickr API : 我的控制器ArticleController.js
尝试从Flickr API获取信息:
**
* ArticleController.js
*
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
search: function (req, res) {
var searchImg = req.param("search");
var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
$.getJSON( flickerAPI, {
tags: "mount rainier",
tagmode: "any",
format: "json"
})
.done(function( data ) {
return res.send(data);
});
}
};
But I got this error : 但是我得到了这个错误:
POST http://localhost:1337/Article/search 500 (Internal Server Error) jquery-1.10.2.js:8706
send jquery-1.10.2.js:8706
jQuery.extend.ajax jquery-1.10.2.js:8136
(anonymous function) main.js:7
jQuery.event.dispatch jquery-1.10.2.js:5095
elemData.handle
Of course I tested my controller without getJSON call and it works fine, returning simple string or JSON values. 当然,我在没有getJSON调用的情况下测试了我的控制器,它可以正常工作,返回简单的字符串或JSON值。
You used JQuery ($.getJson) in your Sails-App - that isn't possible the way you want to. 您在Sails-App中使用了JQuery($ .getJson)-这是您无法实现的。
JQuery uses the Browser-API to make a request, in NodeJs you don't have a browser-API and you don't have JQuery in Sails. JQuery使用浏览器API发出请求,在NodeJ中,您没有浏览器API,并且Sails中没有JQuery。
Replace your jQuery-Code with a HTTP-Request in NodeJS. 用NodeJS中的HTTP请求替换您的jQuery代码。 There is a really good example in the node documentation for that: http://nodejs.org/docs/v0.4.11/api/http.html#http.request 节点文档中有一个非常好的示例: http : //nodejs.org/docs/v0.4.11/api/http.html#http.request
Or you could use a library like request ( https://github.com/mikeal/request ) 或者您可以使用诸如request之类的库( https://github.com/mikeal/request )
You can't use jQuery inside nodejs : it's a browser (client-side) library ! 您不能在nodejs内使用jQuery:这是一个浏览器(客户端)库!
The simplest replacement to your jQuery call would be to use the super simple and super powerfull request
module : https://github.com/mikeal/request jQuery调用的最简单替代方法是使用超级简单且超级强大的request
模块: https : //github.com/mikeal/request
But, if you really want to use jQuery inside nodejs, there is also a few solutions out there.. These really aren't the recommanded solutions to make a simple API call. 但是,如果您真的想在nodejs内使用jQuery,那么这里还有一些解决方案。这些实际上并不是推荐的实现简单API调用的解决方案。 But they can be used to do some scraping : parse an HTML page with all the power of jQuery. 但是它们可以用来做一些抓取工作:使用jQuery的所有功能来解析HTML页面。
In that eventuality, you can give a try to my buck
module, and give me some feedback.. 万一发生这种情况,您可以尝试一下我的buck
模块,并给我一些反馈。
var $ = require('buck');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.