简体   繁体   English

AJAX POST请求在选项中使用变量

[英]AJAX POST request Using Variables in the options

I'm having an issue with making a post request with a project I created to take in contact info from a form. 我在为从表单中获取联系信息而创建的项目发出发布请求时遇到问题。 When I run the function to make the post request I get nothing back in the console. 当我运行该函数以发出发布请求时,控制台中什么也没有返回。 heres the code: 这是代码:

function postContact(token) {
firstName = $('#agentfirst').val();
lastName = $('#agentlast').val();
workNumber = $('#agentwork').val();
cellNumber = $('#agentcell').val();
faxNumber = $('#agentfax').val();
agentEmail = $('#agentemail').val();
agentLicense = $('#agentlicense').val();
companyName = $('#companyname').val();
streetAddress = $('#companyaddress').val();
addressCity = $('#companycity').val();
addressState = $('#companystate').val();
addressZip = $('#companyzip').val();
companyLicense = $('#companylicense').val();

var data = `{
"categoryId": 9,
"companyName": "${companyName}",
"personalContactLicense": {
"licenseNumber": "${companyLicense}"
},
"businessContactLicense": {
"licenseNumber": "${agentLicense}"
},
"noSpam": true,
"firstName": "${firstName}",
"lastName": "${lastName}",
"accessLevel": "1",
"currentMailingAddress": {
"street1": "${streetAddress}",
"city": "${addressCity}",
"state": "${addressState}",
"zip": "${addressZip}"
},
"workPhone": "${workNumber}",
"mobilePhone": "${cellNumber}",
"faxNumber": "${faxNumber}",
"businessEmail": "${agentEmail}"
}`;

var options = {
"async": true,
"crossDomain": true,
"url": "https://api.elliemae.com/encompass/v1/businessContacts",
"method": "POST",
"headers": {
"Content-Type": "application/javascript",
"Authorization": token
},
"processData": true,
"data": data
};
console.log(options.data);

$.ajax(options).done(function (response) {
console.log(response);
});
}

I dont if the problem is me using varibles in the options for the ajax call but any help or being pointed in the right direction would greatly appreciated. 如果问题是我在ajax调用的选项中使用了变量,我不会,但是任何帮助或指向正确的方向将不胜感激。

If you have form, you can simply do 如果您有表格,则只需

var form_data = $('#yourformId').serialize();

And on your ajax options, simply include this 在您的ajax选项上,只需包括

data: form_data,

I am not sure, how you are handling this ajax call and how you are preparing your response. 我不确定您如何处理此ajax调用以及如何准备响应。 You can debug to see where you is the problem. 您可以调试以查看问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM