2013-06-04 47 views
5

Tôi muốn biết tại sao việc đăng một chuỗi JSON đơn giản trong một /:parameter là rất khó để khôi phục. Tôi đã theo nhiều ví dụ nhưng không tìm thấy bất cứ điều gì cụ thể.Đăng đối tượng JSON JSON vào NodeJs Phục hồi

Tôi có mã sau trong giao diện người dùng.

$("#btnDoTest").click(function() { 

    var jData = { 
     hello: "world" 
    }; 
    var request = $.ajax({ 
     url: "http://localhost:8081/j/", 
     async: false, 
     type: "POST", 
     data: JSON.stringify(jData), 
     contentType: "application/javascript", 
     dataType: "json" 
    }); 


    request.success(function(result) { 

     console.log(result); 

    }); 

    request.fail(function(jqXHR, textStatus) { 
     alert("Request failed: " + textStatus); 
    }); 


}); 

Tôi thành công trong việc gửi văn bản đơn giản nếu tôi nối thông số sau j/. Nhưng những gì tôi muốn gửi là một đối tượng như thế này {hello:"world"} và tái tạo lại nó trong nodeJS và làm việc với nó.

--edit:

This is my nodejs file 
/* the below function is from restifylib/response.js */ 
var restify = require("restify"); 

/* create the restify server */ 
var server = restify.createServer({ 

}); 


server.use(restify.bodyParser({ mapParams: true })); 

server.use(
    function crossOrigin(req,res,next){ 
    res.header("Access-Control-Allow-Origin", "*"); 
    res.header("Access-Control-Allow-Headers", "X-Requested-With"); 
    return next(); 
    } 
); 


server.post('/j/', function (req, res, next) { 


    //res.send(201,"REceived body: "+JSON.stringify(req.params)); 
    res.send(201,"REceived body: "+JSON.stringify(req.params)); 
    return next(); 
}); 


var port = 8081; 
server.listen(port); 
console.log("Server listening on port " +port) 

Bất kỳ trợ giúp sẽ được đánh giá cao nhờ.

0x

Trả lời

6

Cuối cùng tôi đã làm việc đó.

--Front mã kết thúc

$("#btnDoTest").click(function() { 



     var request = $.ajax({ 

      url: "http://localhost:3000/j", 
      async: false, 
      type: "POST", 
      data: { 
       blob: {wob:"1",job:"2", ar:[1,2,{a:'b'}]} 
      }, 

      contentType: "application/x-www-form-urlencoded", //This is what made the difference. 
      dataType: "json", 

     }); 


     request.success(function(result) { 

      console.log(result); 

     }); 

     request.fail(function(jqXHR, textStatus) { 
      alert("Request failed: " + textStatus); 
     }); 


    }); 

NodeJs dịch vụ

/* the below function is from restifylib/response.js */ 
var restify = require("restify"); 

/* create the restify server */ 
var server = restify.createServer({ 

}); 


server.use(restify.bodyParser()); 
server.use(restify.CORS()); 


server.post('/j/', function(req, res, next) { 

    res.header("Access-Control-Allow-Origin", "*"); 
    res.header("Access-Control-Allow-Headers", "X-Requested-With"); 

    // req.params == data on jquery ajax request. 


    res.send(200, JSON.stringify(req.params)); 
    console.log(req.params.blob.ar[2].a) 



    res.end(); 
    return next(); 
}); 


var port = 3000; 
server.listen(port); 
console.log("Server listening on port " + port) 
+0

Cảm ơn, 'contentType: "application/x-www-form-urlencoded "'là những gì làm việc cho tôi quá. –

0

Không xâu chuỗi nó. Hãy thử điều này, lưu ý hai thay đổi, tôi đã xóa JSON.stringify và chuyển sang application/json, dưới dạng JSON của nó chứ không phải JavaScript.

var request = $.ajax({ 
    url: "http://localhost:8081/j/", 
    async: false, 
    type: "POST", 
    data: jData, 
    contentType: "application/json", 
    dataType: "json" 
}); 

application/javascript chỉ nên được sử dụng khi thực hiện JSONP.

+0

Tôi đã thử một cái gì đó như thế này. Nó không hoạt động. Tôi thấy một số hành vi lạ trong bài kể từ khi nó được yêu cầu như thế này: /OPTIONS/j – Oxnigarth

+0

Có lẽ quá muộn nhưng nếu bạn vẫn không biết thử sử dụng crossDomain: true như một tùy chọn trên ajax của bạn – albertpeiro

0

câu trả lời của tôi đầu tiên!

jquery:

$.ajax({ 
    url: url, 
    method: 'post', 
    data: JSON.stringify({key:value}), 
    contentType: "application/json" 
}); 

nút http:

server.post('/1', function(req, res) { 
    var body = req.body; 
    var dataValue = body.dataKey; 
}); 

tại sao?

dữ liệu $ .ajax chỉ dành cho những gì để gửi đến máy chủ kết thúc, datatype của nó vẫn chưa được xác định, vì vậy khi sử dụng JSON.stringify({key:value}), dữ liệu sẽ được gửi dưới dạng một chuỗi như '{then chốt: 'xxx'}', và nút nhận một chuỗi, không phải là một đối tượng json ngay cả cấu trúc chuỗi trông giống như một json. nhưng sau khi chúng tôi thêm contentType: "application/json" vào $ .ajax, khi nút nhận dữ liệu, nó sẽ là dữ liệu kiểu đối tượng json thực.

Các vấn đề liên quan