javascript - REST api GET with jQuery, not working -
here code
html:
<form name="test" id="test" method="post"> <input type="submit" name="sub" value="hit me" class="page"/> </form>
jquery:
$.ajax ({ type: "get", url: "https:// url", datatype: 'json', async: false, headers: {"authorization": "bearer xxxx"}, success: function (){ alert('yay!'); } });
result:
{"error": "shell form not validate{'html_initial_name': u'initial-js_lib', 'form': <mooshell.forms.shellform object @ 0x260c6d0>, 'html_name': 'js_lib', 'html_initial_id': u'initial-id_js_lib', 'label': u'js lib', 'field': <django.forms.models.modelchoicefield object @ 0x260c7d0>, 'help_text': '', 'name': 'js_lib'}{'html_initial_name': u'initial-js_wrap', 'form': <mooshell.forms.shellform object @ 0x260c6d0>, 'html_name': 'js_wrap', 'html_initial_id': u'initial-id_js_wrap', 'label': u'js wrap', 'field': <django.forms.fields.typedchoicefield object @ 0x260c190>, 'help_text': '', 'name': 'js_wrap'}"}
now, when inspect element in networks tab of chrome, "403 forbidden". im guessing authorization header incorrect, im not quite sure how it. tips/help?
also have response json in function() success: part?
thanks!
by way have tried this
beforesend: function (xhr){ xhr.setrequestheader('authorization', "bearer xxx"); },
error message in network console
get (url) 403 (forbidden) jquery-1.9.1.js:8526 xmlhttprequest cannot load (url). no 'access-control-allow-origin' header present on requested resource. origin 'http://fiddle.jshell.net' therefore not allowed access.
you possibly try in beforesend
:
beforesend: function (xhr) { xhr.withcredentials = true; xhr.setrequestheader("authorization", "bearer xxxx"); },
and stated in comment, access response you'd use:
success: function (data) { console.log(data); }
let me know how goes. might not getting authorized correctly, inf3rno
suggested, give api docs read through see you're doing correctly :)
Comments
Post a Comment