Access-Control-Allow-Origin header is missing from the requested resource

DISCLAIMER: This question is a question about question. So that makes this question a meta question. It does not have any connection to the previously asked questions. If you find any resemblance, lemme tell you one thing- it purely coincidental.

      

I want to make an AJAX request from my webpage. I tried to do this, but none of the methods worked perfectly. The only post that I have found something close to reality is this .

I tried other methods from SO and other similar sites, but all of these posts were only talking about me.

"No 'Access-Control-Allow-Origin' header is present on the requested resource."

      

Now I know that you will be marking this question as duplicate as there are many questions like this. Now .. Tell me one thing. I tried every piece of sh * t I found on SO, but none of them gave me the result I was looking for. This is not because they are all wrong. This is because I don't know how to use them. Then finally ... I settled on the link above. It's easy ... but I need to know a thing or two about the code. This is the first time I hear the beautiful acronym CORS. So, if anyone can help me understand the issues I am raising, a fraction of the vote for all of me. "I want to resolve this issue with my son on the day before I celebrate my birthday for the third time this year. I will tell you what I have - in the form of resources and questions.

1) A rotten server located at Elizabeth town.
2) I need to access it.
3) I am planning to make a HTTP GET request.
4) I have a url. (eg. http://whereismyweed.com)
5) I store it into a JavaScript variable. var stoner='http://whereismyweed.com'
6) I have a HTML div tag in my webpage. (<div id="myprecious"></div>)
7) I wanna display the response I get from the server inside of 'myprecious' div.
8) And last but not the least... my AJAX function. (Courtesy: some website I visited)




$.ajax({
                url: stoner,
                    data: myData,
                    type: 'GET',
                    crossDomain: true, // enable this
                    dataType: 'jsonp',
                    success: function() { alert("Success"); },
                error: function() { alert('Failed!'); },
                beforeSend: setHeader
            });

      

What is 'myData'? What does it contain. How can I get a response to this request? What is "setHeader"? Does it matter? How can I display the response inside myprecious div? What changes should I make to the function? Is this function correct?

Too many questions, right ???? Well ... I only need one general answer?

+3


source to share


1 answer


Your function is correct. The back of the steps reaches its goal -



 //for getting response modify your code like 

      success:function(response){
            alert(response);
            $('#myprecious').html(response); //myprecious is id of div
     }

  // myData variable is jSon object which contains request parameter has to send.Eg.
     var myData = {'first_name':'Foo','last_name':'Bar'}  // now on server first_name and last_name treated as request parameter.


  // If server not required any special headers to validate request 'setHeader' does not require. by default $.ajax will take care of it. you can remove it.



     /// final code looks like 


         $.ajax({
            url: stoner,
                data: myData, 
                type: 'GET',
                crossDomain: true, // enable this
                dataType: 'jsonp',
                success: function(response ) { $('#myprecious').html(response);    
  },
                error: function() { alert('Failed!'); }

        });

      

-1


source







All Articles