This topic contains 9 replies, has 0 voices, and was last updated by CREECE 7 years, 7 months ago.
-
AuthorPosts
-
September 29, 2016 at 7:17 pm #6136
CREECEI have a very trivial suitelet (anoymous – available without login) that just returns an object:
Code:
function startSuitelet(request, response) {
nlapiLogExecution(‘DEBUG’, ‘suitelet request’, request.getBody());response.write(JSON.stringify({‘status’: ‘testing’}));
}
I am trying to consume this via Ajax in a HipChat sidebar app I am trying to write:Code:
$.ajax({
type: “GET”,
url: ‘my_suitelet_public_url’,
dataType: ‘jsonp’,
success: function (data) {
alert(JSON.stringify(data));
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(“error: ” + errorThrown + ‘ XMLHttpRequest: ‘ + JSON.stringify(XMLHttpRequest) + ‘ textStatus: ‘ + textStatus);
}
});
This seems super trivial but no matter what I try in the Ajax query, it never works. This isn’t something I’ve done before so maybe I am a little off? I do NOT want to use a RESTlet as I don’t care about logging in for this but I would venture to say that RESTlets have the same issue.I CAN however, post to this using POSTman in chrome -or- click the Suitelet public URL and get my JSON data so I know it is working. I can also see my suitelet being hit when i make the JSON call but it never makes it back to my code. It always goes into the error branch with nothing useful as it says it was successful yet still errors…
The error I am receiving as output from this:
with jsonp as dataType:
error: Error: jQuery21104258091155789343_1475198345974 was not called XMLHttpRequest: {“readyState”:4,”status”:200,”statusText”:”load “} textStatus: parsererror
with json as dataType: error: XMLHttpRequest: {“readyState”:0,”status”:0,”statusText”:”error” } textStatus: error
Thanks for the assistance!
9/30 Update… Solved it
You cannot use “json” or “application/json” due to the same origin policy (https://en.wikipedia.org/wiki/Same-origin_policy). You have to specify jsonp in your ajax code. Here is a working example:
Suitelet:
Code:
function startSuitelet(request, response) {
var callback = ‘doSomething’; // or you could pass this in as a param
var result = getMyData();
var strJson = callback+'(‘ + JSON.stringify(result) + ‘);’;response.setContentType(‘JAVASCRIPT’)
response.write(strJson);
}
Node.js AJAX code:Code:
function doSomething(data) {
// Whatever you want to do with Suitelet’s return data
}$.ajax({
url: ‘SUITELET_URL’,
dataType: “jsonp”,
jsonpCallback: ‘doSomething’,
success: function(data) {
doSomething(data);
}
});
This is a cached copy. Click here to see the original post. -
September 29, 2016 at 7:55 pm #6137
errol2 things I see:
1) I would set the content type of your suitelet to JSON using response.setContentType(‘JSON’); (put this line before your response.write line)
2) In the success function of the AJAX, I don’t believe you should be doing JSON.stringify(data). Try just logging data as is to see what is returned.
-
September 29, 2016 at 8:09 pm #6138
CREECEI had already tried both JSON and JAVASCRIPT when setting the content type and no go. Also setting dataType to json / jsonp still gives the exact same errors as I stated above.
I am 99.9% sure this is some kind of cross-domain weird issue. I have no idea how to resolve it though.
If i just use json as the dataType and try to call via jquery API website:
No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://api.jquery.com’ is therefore not allowed access.
if I add a proxy to the url http://cors.io/? it works fine in the browser….. No luck so far with my Hipchat app code but at least got something going in the browser.. if you DO NOT want to use a proxy, you’ll have to specify jsonp as the dataType and then specify a callback in the suitelet but that seems to only work client side for me right now.
-
September 29, 2016 at 10:57 pm #6139
david.smithSet the contentType: “application/json”, in your Ajax call.
-
September 30, 2016 at 3:03 pm #6140
CREECEI did get this working finally.
You cannot use “json” or “application/json” due to the same origin policy (https://en.wikipedia.org/wiki/Same-origin_policy). You have to specify jsonp in your ajax code. Here is a working example:
Suitelet:
Code:
function startSuitelet(request, response) {
var callback = ‘doSomething’; // or you could pass this in as a param
var result = getMyData();
var strJson = callback+'(‘ + JSON.stringify(result) + ‘);’;response.setContentType(‘JAVASCRIPT’)
response.write(strJson);
}
Node.js AJAX code:Code:
function doSomething(data) {
// Whatever you want to do with Suitelet’s return data
}$.ajax({
url: ‘SUITELET_URL’,
dataType: “jsonp”,
jsonpCallback: ‘doSomething’,
success: function(data) {
doSomething(data);
}
});
Thanks
ironside replied on 10/02/2016, 11:02 AM: jsonp is a hack that needs to die.
-
September 30, 2016 at 3:16 pm #6141
david.smithChris,
Where are you calling the suitelet from? You make it sound like this is from another UI page inside of NetSuite? This is basically what I do…
Code:
jQuery.post(‘url’,{“something”:”my post data”}, function(resultdata){
console.log(resultdata);
}, ‘json’); -
September 30, 2016 at 4:00 pm #6142
CREECEThis is from an application outside of netsuite. Your example would work fine if it were inside NetSuite. My example is how to call a NetSuite resource (suitelet specifically) outside of the NetSuite domain.
david.smith replied on 09/30/2016, 04:28 PM: ok, gottcha
-
October 1, 2016 at 11:24 am #6143
CREECEWhen it is ready (Blizzard Entertainment ™). It is an internal application but i would be happy to show you
-
March 29, 2017 at 7:47 pm #6144
philip.lau@lollicup.comHi I am new in here… Is it possible to called Jsonp using Restlet script. How do I overcome the multiple origin error?
-
March 29, 2017 at 10:04 pm #6145
CREECEIn order to get it around it in my app, i made HTML w/ the fields I needed available in the netsuite file cabinet w/o login and loaded them, did my work in that html and redirected within netsuite. Since the HTML files are already loaded on netsuites domain, it worked just fine for me.
-
AuthorPosts
You must be logged in to reply to this topic.