Forum Discussion
Wei-Painter
Nov 21, 2024Copper Contributor
SharePoint 2019 On Premise: JavaScript <REST API>: 400 (Bad Request)
Hi there,
I try to use the CAML w/ SharePoint REST API to extract the data from the List. And the JavaScript like this as follows. But unfortunately, when the REST API was performed, and error message played out. Has anyone encountered such a problem before and can provide relevant solutions? Because I spent a lot of time on this problem, thank you
url: "/_api/web/lists(guid'98495B1F-2BD1-4B09-80CA-4A4FE5EF82ED')/GetItems",
method: 'POST'
400 (Bad Request) >> Error
url: "/_api/web/lists(guid'98495B1F-2BD1-4B09-80CA-4A4FE5EF82ED')",
method: 'POST'
WORK FINE. No issue.
$(document).ready(function() {var projectCodeDropdownId = '#Project\\_x0020\\_Code\\_576bd655\\-c475\\-412d\\-bd61\\-8164571f6726\\_\\$LookupField';var tasksCodeDropdownId = '#Tasks\\_09d67b50\\-12ae\\-4d0c\\-b3e0\\-b83d615baa8a\\_\\$LookupField';var fallbackSelector = 'select[id*="Project_x0020_Code"]';console.log('Script loaded'); // Debugging statement$(projectCodeDropdownId + ', ' + fallbackSelector).change(function() {console.log('Dropdown changed'); // Debugging statementvar selectedProjectCode = $(this).val();var selectedProjectText = $(this).find('option:selected').text();console.log('Selected Project Code:', selectedProjectCode); // Debugging statementconsole.log('Selected Project Text:', selectedProjectText); // Debugging statementvar taskPrefix = '';// Determine the task prefix based on the project code textif (selectedProjectText.startsWith('BM')) {taskPrefix = 'A';} else if (selectedProjectText.startswith('S')) {taskPrefix = 'S';}console.log('Task Prefix:', taskPrefix); // Debugging statement// 動態生成 CAML 查詢var camlQuery = `<View><Query><Where><Contains><FieldRef Name='Title' /><Value Type='Text'>${taskPrefix}</Value></Contains></Where></Query></View>`;// var listId = '98495B1F-2BD1-4B09-80CA-4A4FE5EF82ED';// var listName = '執行項目維護';// Filter tasks based on the determined prefix$.ajax({url: "/_api/web/lists(guid'98495B1F-2BD1-4B09-80CA-4A4FE5EF82ED')/GetItems",method: 'POST',headers: {'Accept': 'application/json;odata=verbose','contentType': 'application/json;odata=verbose','X-RequestDigest': $('#__REQUESTDIGEST').val()},data: JSON.stringify({query: {__metadata: { type: 'SP.CamlQuery' },ViewXml: camlQuery}}),success: function(data) {console.log(data); // 檢查返回的資料結構var filteredItems = data.d.results;var tasksDropdown = $('#Tasks\\_09d67b50\\-12ae\\-4d0c\\-b3e0\\-b83d615baa8a\\_\\$LookupField'); // 確保這裡的 ID 正確tasksDropdown.empty();$.each(filteredItems, function(index, task) {tasksDropdown.append('<option value="' + task.Title + '">' + task.Title + '</option>');});},error: function(xhr, status, error) {console.error("Error fetching items: ", error);console.error("Status: ", status);console.error("Response: ", xhr.responseText); }});});});
http://win-0ggeu9kbm60:19027/_api/web/lists(guid'98495B1F-2BD1-4B09-80CA-4A4FE5EF82ED')/GetItems 400 (Bad Request)
No RepliesBe the first to reply