我尝试执行GET请求以返回单个"项目",但是当我将参数发送给Rails时,他们会被拒绝,因为它已经显示作为[object Object],而不是Rails期望的参数。
之前这对我有用,所以我有点失落。我应该能够输入一个代表项目ID的数字,它应该返回该项目。以下是我输入ID的index.html代码:
const onGetProject = function (event) {
event.preventDefault()
const data = getFormFields(this)
api.getProject(data)
.then(ui.getProjectSuccess)
.catch(ui.getProjectFailure)
}
事件在提交时触发:
const getProject = (id) => {
event.preventDefault()
return $.ajax({
url: config.apiOrigin + '/projects/' + id,
method: 'GET',
headers: {
Authorization: 'Token token=' + store.user.token
}
})
}
和onGetProject抓取数据并将其发送到API调用:
Started GET "/projects/[object%20Object]" for 127.0.0.1 at 2017-06-27
22:03:35 -0400
Processing by ProjectsController#show as */*
Parameters: {"id"=>"[object Object]"}
API调用:
API="${API_ORIGIN:-http://localhost:4741}"
URL_PATH="/projects/${ID}"
curl "${API}${URL_PATH}" \
--include \
--request GET \
--header "Content-Type: application/json" \
--header "Authorization: Token token=$TOKEN" \
--data '{
"project": {
"id": "'"${INTEGER}"'"
}
}'
这是终端显示的内容:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void getInformationKeyBoard(int, string[], int[]);
bool openFile(ifstream &infile, string fileName);
void display(int size, string array[]);
void read2Array(ifstream &infile, int size, string array[]);
void printReport(string name[], int score[], int NumberOfStudent);
int main()
{
const int size = 1024;
string Name[size], scoreFile[size];
int score[size];
int NumberOfStudent;
const int SIZE = 7;
ifstream inFile;
char choice;
cout << "You want to enter your scores by your keyboard (A) or from your input (B): ";
cin >> choice;
if (choice == 'a' || choice == 'A') // It will take information from keyboard
{
cout << "How many students do you want to enter: ";
cin >> NumberOfStudent;
getInformationKeyBoard(NumberOfStudent, Name, score);
printReport(Name, score, NumberOfStudent);
}
else if (choice == 'b' || choice == 'B') // It will take information from file
{
string name;
char again = 'Y';
bool close = false;
cout << "Enter name of file: ";
cin >> name;
openFile(inFile, name);
read2Array(inFile, SIZE, scoreFile);
display(SIZE, scoreFile);
}
else // If you choice is not A,a or B,b
cout << "Your did not follow the right instruction.";
cout << endl << endl;
system("pause");
return 0;
}
// Open file
bool openFile(ifstream &infile, string fileName){
infile.open(fileName);
if (infile)
return true;
return false;
}
void getInformationKeyBoard(int size, string Names[], int scores[]) // Information from keyboard
{
for (int i = 0; i < size; i++)
{
cout << i + 1 << ". Student First Name and Last Name: ";
cin.ignore();
getline(cin, Names[i]);
do
{
cout << i + 1 << ". Enter the score between 1 and 100: ";
cin >> scores[i];
} while (scores[i] > 100 || scores[i] < 0);
}
}
void read2Array(ifstream &infile, int size, string array[]){
int index = 0;
string line;
while (getline(infile, line)){
array[index] = line;
++index;
}
}
// Display array
void display(int size, string array[]){
for (int index = 0; index < size; ++index){
cout << array[index] << endl;
}
}
void printReport(string name[], int score[], int NumberOfStudent)
{
int lowest, highest, mean;
cout << "Enter lowest score: ";
cin >> lowest;
cout << "Enter highest score: ";
cin >> highest;
cout << "Enter mean score: ";
cin >> mean;
cout << "================================================================================";
cout << setw(10) << "Number of scores = " << NumberOfStudent << endl;
cout << setw(10) << "Lowest Score = " << lowest << endl;
cout << setw(10) << "Highest Score = " << highest << endl;
cout << setw(10) << "Mean Score = " << mean << endl;
cout << "Name" << setw(15) << "Score" << setw(15) << "IsLowest" << setw(15) << "IsHighest" << setw(15) << "Mean" << endl;
cout << "-----------------------------------------------------------------" << endl;
cout << name[NumberOfStudent] << endl;
cout << endl;
}
我编写了一个脚本,该脚本应该为单个ID执行GET请求,但这会返回所有内容,而不是&#34; show&#34;我希望Rails能够执行:
脚本:
Number of scores = 3
Lowest Score = 82
Highest Score = 92
Mean Score = 87
Name Score IsLowest IsHighest >=Mean
F1 L1 82 Y N N
F2 L2 87 N N Y
F3 L3 92 N Y Y
我在我的后端使用Ruby on Rails,并且我能够成功执行GET请求,该请求返回所有Projects及其嵌套任务的索引,如预期的那样。但是,我无法获得单个GET请求,并且不确定我的代码可能出现了什么问题。
如果您需要任何其他代码或信息,请与我们联系。谢谢你的帮助!
答案 0 :(得分:0)
由于ID是一个对象,我需要使用id.project.id
更明确地访问该值。
一旦我在我的API调用中替换了它,它就成功执行了,并且返回了我请求的单个Project。这是工作代码:
const getProject = (id) => {
event.preventDefault()
return $.ajax({
url: config.apiOrigin + '/projects/' + id.project.id,
method: 'GET',
headers: {
Authorization: 'Token token=' + store.user.token
}
})
}
谢谢Patrick Evans!