尝试将JSON发布到URL上

时间:2014-08-10 08:17:50

标签: web-services

我在申请流程的第2步。第一步是在URL上使用GET方法并检索名称和电子邮件地址。

以下是网址: https://mkpartners.secure.force.com/services/apexrest/careers?firstName=Homer&lastName=Simpson&email=HSimpson@Springfield

现在我被要求POST一个JSON对象。这是问题所在:

Congratulations John Quach, you completed Step 1!

Step 2 is as follows:
Perform a POST to the URL you constructed in Step 1.
You will need to post an instance of the application object described below.
You will need to post the body in properly formatted JSON.
You will need to pass in all required headers.
Please set the isTest boolean to TRUE while testing, and FALSE when you are ready for your final submission.
application {
Boolean isTest (required)
String firstName (required)
String lastName (required)
String email (required)
String phone (required)
String zipcode (required)
String describeYourself (required)
ool[] objectLanguages (required)
education[] education (required)
experience[] experience (required)
certification[] certs
}

education {
String school (required)
Integer graduationYear (required)
String degree (required)
String major (required)
}

experience {
String company (required)
Date fromDate [yyyy-MM-dd] (required)
Date toDate [yyyy-MM-dd] (required)
String title (required)
String workDone (required)
}

certification {
String certification (required)
Date dateCertified [yyyy-MM-dd] (required)
}

ool {
String language (required) [must include at least 'javascript'; include any other OOP languages you know]
Integer proficiency (required) [scale of 0-10, 0 being none, 10 being proficient]
}

A successful post will result in a code 202 and you will receive an automated email confirmation. Good Luck!

我尝试了很多代码。我已按照您的建议,并根据您提供的代码使用了此建议。我通过HTML页面使用javascript。

 <script> 
var webLink = "https://mkpartners.secure.force.com/services/apexrest/careers?firstName=John&lastName=Quach&email=johnq1216@gmail.com"; 


var apply = {
         isTest : false,
         firstName : "John",
         lastName : "Quach",
         email : "johnq1216@gmail.com",
         phone : 6269355016,
         zipcode : 91207,
         describeYourself : "Self Taught Programmer and Developer"
    }


function httpPost(theUrl) {
    var xmlHttp = null;
    xmlHttp = new XMLHttpRequest();         
    xmlHttp.open( "POST", theUrl, false);    
    xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); 
    xmlHttp.send(JSON.stringify(apply)); 
}  


httpPost(webLink); 
</script>
//This script will make a POST request. Read above comments.

当然没有一个有效。我究竟做错了什么?我是否需要嵌套了语言,教育和认证对象的完整JSON对象?我之所以离开它是因为我想从网站上得到一些回复,我想保持答案很小。

我是否需要在计算机上安装一个记事本才能将此JSON对象发布到URL?

2 个答案:

答案 0 :(得分:1)

//You will still have to do the JSON encoding. Use the JSON encoder for this and then make the request. Pass the JSON object you get after processing into http post as a parameter.
<script> 
var webLink = "https://mkpartners.secure.force.com/services/apexrest/careers"; 
var moreSimplified = { firstName : "John", lastName : "Quach", email : "johnq1216@gmail.com" } 
function httpPost(theUrl) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();         
xmlHttp.open( "POST", theUrl, false);    
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); 
xmlHttp.send(JSON.stringify(moreSimplified)); 
}  
httpPost(webLink); 
</script>
//This script will make a POST request. Read above comments.

答案 1 :(得分:0)

您的json无效

var json =   {
    firstName : "John",
    lastName : "Quach",
    email : "johnq1216@gmail.com"
};

xmlhttp.send(string)仅适用于POST动词

xmlhttp.open("POST", ,"demo_post.asp",true);
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.send(JSON.stringify(json);