-1

I have the following ajax call

var response = "";
//Ajax call to the DB server
var target_url = "http://SomeIP/db/*create_table*?token=1234qwer&table_name="+table_name+"&array_of_fields={"+final_fields+"}";
function hit_db(callback)
{
    $.ajax({
        url: target_url,
        type: 'GET',
        async: true,
        crossDomain: true,
        success: callback,
        });
}
hit_db(function(data){
    response = data;
    alert(response);
});

However alert(reponse) shows undefined when the correct output should be ERROR table already exists

However, when I check on my dev. tools I see the following

enter image description here

and If I click on the link of the Uncaught SyntaxError it takes me to this page where the correct output is displayed

enter image description here

Why is the correct output I want in that Unexpected Identifier but I get undefined alert(response) ? Any possible fixes?

EDIT------

Here's some simplified code and the response

function hit_db(callback)
{
    $.ajax({
        url: target_url,
        type: 'GET',
        success: callback,
        });
}
hit_db(function(data){
    response = data;
    console.log(response);
});

enter image description here

---Network tab enter image description here enter image description here

Thank you!!

sideshowbarker
  • 62,215
  • 21
  • 143
  • 153
net30
  • 177
  • 10
  • Sorry i changed my question last minute. I updated to my latest coded. The screenshot is the exact response I get when i check my dev tools. But when I alert(response) I get undefined – net30 May 29 '18 at 01:53
  • Also would you mind editing out the IP in your comment please? I just want to eliminate the chance of people trolling with my server hehehe – net30 May 29 '18 at 01:54
  • 1
    what looks to be the issue is that the server does not respond with JSONP - can you show an exact example of the response you get? Are you sure you are responding with JSONP not just JSON? or perhaps the JSONP is invalid (it has a syntax error in it, like the error message states) – Jaromanda X May 29 '18 at 01:58
  • I eliminated the jsonp to see if it would change anything but the result is still the same. Its driving me crazy because I dont know where to look for the syntax error – net30 May 29 '18 at 02:16
  • look in the response in the browser developer tools network tab - does it look valid? – Jaromanda X May 29 '18 at 02:27
  • I just checked the network tab and the correct response is there. I just saw that the `content-type` of the response headers is `text/html charset=utf-8` does that impact anything? How could I properly catch that? – net30 May 29 '18 at 02:34
  • 1
    So, you use JSONP because JSON gives you CORS errors - but JSONP isn't JSON, it's JSONP ... hence the syntax error - but as you've shown absolutely NOTHING about the response data, only claiming "it is correct", then it's hard to help. The response data is **not** correct for JSONP, that's why you have a syntax error, because JSONP isn't JSON, but I'm guessing you haven't sent JSONP from the server at all' – Jaromanda X May 29 '18 at 02:42
  • I apologize :( I should have provided screenshots sooner and I really do appreciate the time you're taking to help. I have provided screenshots of my network tab and I took out JSONP out of my code but I still get the same results – net30 May 29 '18 at 02:53
  • So without data type jsonp you still get a syntax error? – Jaromanda X May 29 '18 at 02:57
  • No, now I just get an `undefined` alert – net30 May 29 '18 at 02:59
  • that's due to the CORS error – Jaromanda X May 29 '18 at 03:01
  • Even though I get the desired output in the network tab? I put on my last screenshot – net30 May 29 '18 at 03:05
  • 1
    yes - the developer tools show the response - but javascript can not access the response due to lack of CORS headers – Jaromanda X May 29 '18 at 03:06

2 Answers2

1

Can you pls edit the code to a simple ajax call and check the console what it responds. Because if it works simply on a browser, i expect it to be a usual get request.

function hit_db()
{

$.ajax({
  url: target_url,
}).done(function(resp) {
  console.log(resp)
});

}
hit_db();

Edit: The issue was due to cross domain request. You will have to add 'Alloww-cross-orgin-request':* to your server response or you can do this to by pass it in your browser. If bypassing it in your browser is not an option i would say you will need to read about the different condition checks for cross origin checks done by browser in this link

Rohith Murali
  • 4,864
  • 2
  • 18
  • 23
  • I have added the edit with the simplified code – net30 May 29 '18 at 02:16
  • And what do you see in console log?? – Rohith Murali May 29 '18 at 02:16
  • The console is empty. Nothing is logged – net30 May 29 '18 at 02:20
  • Can you verify in the network tab that your api call has been requested? And if there is an error, pls paste it here,also confirm that the request was made in the correct url – Rohith Murali May 29 '18 at 02:22
  • Yes, I confirmed that is the correct URL with Postman which returns the correct output. And there are no errors in the network tab – net30 May 29 '18 at 02:25
  • Not the postman, when you run this code, you can see this api call in your browser developer tools's network tab. I was referring to that entry.Please verrify that the api call was made to correct url and whther there is any error there. – Rohith Murali May 29 '18 at 02:27
  • I just checked the network tab and the correct response is there. I just saw that the content-type of the response headers are `text/html charset=utf-8` does that impact anything? – net30 May 29 '18 at 02:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171946/discussion-between-rohith-murali-and-net30). – Rohith Murali May 29 '18 at 02:36
-2

CORS You can search CORS related content

stone
  • 1
  • 1