Solved

enable cors jquery

Posted on 2015-01-26
8
158 Views
Last Modified: 2015-02-14
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?

Thanks
0
Comment
Question by:ITsolutionWizard
  • 4
  • 2
  • 2
8 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40571918
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
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40571923
So just to confirm, your jQuery page is on a different domain to your IIS server?
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 40573049
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..
0
 

Author Comment

by:ITsolutionWizard
ID: 40573135
any source codes I can read
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 40573287
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.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40573754
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?
0
 

Author Comment

by:ITsolutionWizard
ID: 40580378
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
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40580537
Yes that makes sense but are they served from the same domain?

Eg

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! :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now