3

Any javascript nerds what to come play with me in a devrant-related project? I need help with cross-origin JSON.

Comments
  • 2
    If you look at this plugin, you'll see I used a totally hacky solution, but I wasn't able to get the AJAX calls to play nicely without a cross-site error in the browser. I have a CURL function that works, but I also failed to figure out how to use it to get JSON for the javascript. https://github.com/mmatthews1981/...
  • 0
    Awesome! Like you said, can you just curl/fetch from the API on the backend? I think that should definitely work.
  • 0
    @meredithlynn for simplicity, if you make it a backend call, you can just do file_get_contents on the endpoint and that should get you back the result (then you just need to JSON decode it).
  • 0
    I can submit PR later if you're not able to get it going.
  • 1
    @dfox CURLing works well, but like I said, I'm unsure how to CURL in such a way that the data is available for the javascript to display. I'd just do it all in PHP, but WP dashboard widgets seem to hate the idea, and it's nicer to use with javascript anyway.
  • 1
    @meredithlynn I see what you're saying! I don't think there's any way to hit that endpoint with JS because it will always be cross origin. I can probably put a policy in to allow it but it would have to be a different endpoint.

    Looking here, it seems it seems decently straight forward to add ajax functionality to a WordPress plugin: https://sitepoint.com/adding-ajax-t...
  • 2
    @dfox I'll give it a shot and see what happens.
  • 3
    Sounds fun
  • 1
    CORS Is a PITA but helps secure your browser.....unless the hacker can proxy the calls to make them server side and return the response.
Add Comment