简体   繁体   中英

Not receiving response from php JQuery

I'm trying to receive a response from a php which gives back a response in the following format:

PHP response

 response=1

JQUERY

$.get('getstate.php', {
    email: $('#game-email').val(),
    country: 'DE',
    lang: lang,
    source: 'promotion'
}, function (data) {
    console.log(data);
});

When I load the php in a browser it gives me the correct response in the browser:

getstate.php?email=test@gmail.com&country=DE

but when I try to receive a response and show the value received in the variable data nothing appears. What could I be doing wrong? Thanks!

you have to use jquery utility functions like $.get or $.ajax with type as GET .

If you are able to get result in browser url, then the result is coming for a GET request and here in jquery code you are making a post request.

UPDATED TO COMMENTS

"Origin null is not allowed by Access-Control-Allow-Origin."

This issue comes when you are probably making a ajax request to a page in another domain or when you load the page directly via double clicking on it without going through the host url.

ie instead of file:/// in url address you should have same domain name on which the php page you are requesting.

IF YOU ARE MAKING A CROSS DOMAIN AJAX REQUEST

If your ajax request target page lies in another domain, you can do a Cross domain ajax request

you should set

Access-Control-Allow-Origin: *

header on your server which hosts the ajax target page. Setting the value to * means it will allow requests from any domain, so it makes security venerable as mentioned here .

also at the client end you may use Jquery.ajax utility function with the crossDomain setting set to true. Also this will have issues that it wont work in IE versions.

see some related question describing this issue.

this article on jquery docs, discusses the same issue and has a suggestion mentioned via including a js fix file which i have tried myself before and got working.

Below are some related links for setting the CORS support in various servers and doing the ajax call at client side.

add CORS support to my server

use CORS from JavaScript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM