Hello, Is there a way to make an AJAX call from a ...
# suitescript
m
Hello, Is there a way to make an AJAX call from a User Event script?, similar to this one:
Copy code
<script>
	
	var url = '';
		$.ajax({

			contenType: "application/json; charset=utf-8",
			data: {
			  "key1": "some value",								
			  "key2": 'some value'},
			type: "POST",
			dataType: "json",
			url: 'URL HERE',
			  
			success: function(data,status,xhr){
			console.log(data);
				if(data.error != undefined && data.error != ""){
					  window.alert("Error: " + data.error);
				}else{
					if(data.valid){
					  console.log(data);
					  window.alert(data.id);
					}
					   
				}},

				error: function(xhr,status,error){
					window.alert(error);
				}
				   
		});
	
</script>
r
Try using the N/http or N/https modules
🙌🏼 1
m
@rmhollands Thanks for your reply. I'm not looking to display data on the client-side. I have a User Event script that needs to send data to a web service with an ajax call. I've never done ajax in NetSuite. The web service/API uses https so I'm thinking "https.post.promise(options)" should do the trick?
a
@Mr_Tib That is correct…
🙌🏼 1
m
It did work on first try (Oddly) with "https.post.promise(options)". You learn something new everyday. Thanks @rmhollands & @alien4u
s
I don't think there's any reason to use the
promise
version - just
<http://https.post|https.post>
should be simpler and perform equally.
m
@Shawn Talbert I just tried it with
<http://https.post|https.post>
instead of
promise
and it did work. I'm confused, I thought it wouldn't work. When I tried using Postman to send a POST request it failed so I figured... "Oh, it's an AJAX request so I should use
promise
when doing this from NetSuite"
a
@Mr_Tib You only need to use promise if you need to do something with the response in an asynchronous way, otherwise you are good using it without promise as @Shawn Talbert mentioned.
m
@alien4u got it, thanks. I'm going to stick with the
promise
version because I need to save the response in a custom record.
s
well, they should still get the response - the
promise
version would only be needed if async behavior were needed, but that's not usually very useful for server-side scripts.
if you are going to stick with the
promise
flavor for some reason, be sure to remember to handle errors