enable cors jquery

Look like my jquery may need to enable CORS. But I do not want to update IIS header because I do not want to impact all other stuffs there.

Is it possible to show me how to add CORS on jquery/ajax codes?

Who is Participating?
RobConnect With a Mentor Owner (Aidellio)Commented:
Yes that makes sense but are they served from the same domain?


Www.yourdomain.com shows the webpage but if your web method is on the same domain, ie www.yourdomain.com/WebMethod then you don't need jsonp or cors! :)
RobOwner (Aidellio)Commented:
it HAS to be done server side.  If the cross-origin header allows it then the browser (jquery) will be able to exchange data.

You cannot do this on the client.

Ok... you can get around this with a plugin for your browser, but every one of your clients would need to have a plugin like this.  This one for chrome: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?utm_source=chrome-app-launcher-info-dialog
RobOwner (Aidellio)Commented:
So just to confirm, your jQuery page is on a different domain to your IIS server?
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Chris StanyonCommented:
As Rob has already pointed out, if your jQuery is on a different domain to your AJAX response, then you have to enable CORS AT THE SERVER!

If you can't / won't do this, then your other option is to create a proxy script (PHP for example) on your own server. This script would make the call to the remote server and retrieve the data (not subject to the same CORS policy as Javascript). Your jQuery would then make it's AJAX call to your own script, and you wouldn't have to worry about Cross-Origin..
ITsolutionWizardAuthor Commented:
any source codes I can read
Chris StanyonCommented:
Plenty of examples online - it depends on exactly what you need to achieve.

If your jQuery needs to pass data to the server, then your PHP script will need to read this data - probably from the POST array:

$someValue = $_POST['myData'];

Depending on the remote server, you may just be able to read the data straight in:

$remoteData = file_get_contents('http://someremotedomain.com/');

You may also need to use cURL.

Also depends on how your want to pass the data back to your jQuery call - you could just echo it straight out, but you'll probably need to send it back as JSON:

echo $json_encode($yourData);

You've not given enough info for specifics, so can't offer concrete suggestions.
RobOwner (Aidellio)Commented:
We need a little more to go on than what you've provided.

It's your website running on a different domain than your iis server?
ITsolutionWizardAuthor Commented:
the web services and jquery / ajax consume sides are separated. and the web services is written in .net/c#
the consume side is just html or classic asp
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.