2015-06-05 22 views
8

Tôi đang cố gắng đăng biểu mẫu chứa first_name, last_name, email, mật khẩu và password_confirmation bằng cách sử dụng api tìm nạp phản ứng gốc.Làm cách nào để đăng biểu mẫu bằng cách tìm nạp trong phản hồi gốc?

fetch('http://localhost:3000/auth', { 
    method: 'post', 
    body: JSON.stringify({ 
    config_name: 'default', 
    first_name: this.state.first_name, 
    last_name: this.state.last_name, 
    email: this.state.email, 
    password: this.state.password, 
    password_confirmation: this.state.password_confirmation, 
    }) 
}) 

Output trong Rails console

Parameters: {"{\"config_name\":\"default\",\"first_name\":\"Piyush\",\"last_name\":\"Chauhan\",\"email\":\"[email protected]\",\"password\":\"diehard4\",\"password_confirmation\":\"diehard4\"}"=>"[FILTERED]"} 
Unpermitted parameter: {"config_name":"default","first_name":"Piyush","last_name":"Chauhan","email":"[email protected]","password":"diehard4","password_confirmation":"diehard4"} 

Vì vậy, đăng toàn bộ giá trị của nó như là chuỗi và đường ray được phân tích các chuỗi như là một biến. Tôi muốn loại bỏ "{" khỏi phản hồi json. Làm thế nào để làm nó ?

Trả lời

7

vì vậy nếu tôi hiểu rõ bạn, bạn muốn nó đăng cùng một chuỗi nhưng không có dấu ngoặc nhọn?

Nếu đúng như vậy, bạn chỉ có thể tách chúng ra khỏi chuỗi.

.replace (/ {|}/gi "")

do đó sẽ trông như sau

fetch('http://localhost:3000/auth', { 
method: 'post', 
body: JSON.stringify({ 
    config_name: 'default', 
    first_name: this.state.first_name, 
    last_name: this.state.last_name, 
    email: this.state.email, 
    password: this.state.password, 
    password_confirmation: this.state.password_confirmation, 
    }).replace(/{|}/gi, "") 
}) 
2
const m = encodeURIComponent(userEmail); 
const p = encodeURIComponent(userPassword); 
const requestBody = `email=${m}&pass=${p}`; 
fetch(`http://server.com`, { 
    method: 'POST', 
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 
    body: requestBody 
}) 
+1

Vui lòng cung cấp chi tiết và giải thích thêm – M98

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