Javascript-js cross-origin request

Source: Internet
Author: User
{Code...} js request data failed. Php succeeded. Why? {Code ...}
$.ajax({        url : "http://apis.baidu.com/apistore/iplookupservice/iplookup?ip="+window.location.href,        beforeSend: function(request) {                        request.setRequestHeader("apikey", "1f5cb934618472891347834b2bf64c8d");                    },        type:"GET",        dataType : "jsonp",        success : function(data){            console.log(data);        }    });

Js request data failed.

Php succeeded. Why?

$ Ch = curl_init (); $ url = 'HTTP: // apis.baidu.com/apistore/iplookupservice/iplookup? Ip = '. $ _ SERVER ['remote _ ADDR ']; $ header = array ('apikey: 1f5cb942518472891347834b2bf64c8d',); // Add apikey to header curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // execute the HTTP request curl_setopt ($ ch, CURLOPT_URL, $ url); $ res = curl_exec ($ ch ); $ info = json_decode ($ res );

Reply content:
$.ajax({        url : "http://apis.baidu.com/apistore/iplookupservice/iplookup?ip="+window.location.href,        beforeSend: function(request) {                        request.setRequestHeader("apikey", "1f5cb934618472891347834b2bf64c8d");                    },        type:"GET",        dataType : "jsonp",        success : function(data){            console.log(data);        }    });

Js request data failed.

Php succeeded. Why?

$ Ch = curl_init (); $ url = 'HTTP: // apis.baidu.com/apistore/iplookupservice/iplookup? Ip = '. $ _ SERVER ['remote _ ADDR ']; $ header = array ('apikey: 1f5cb942518472891347834b2bf64c8d',); // Add apikey to header curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // execute the HTTP request curl_setopt ($ ch, CURLOPT_URL, $ url); $ res = curl_exec ($ ch ); $ info = json_decode ($ res );

In jQuery, ajax with the type jsonp does not allow custom request headers. Although you have added the custom header apikey to beforeSend, however, because jsonp is implemented by using the script tag instead of the XMLHttpRequest object, the custom header you set is invalid, so the result returned by Baidu is Missing apikey.
Reference: http://www.jquery123.com/jQuery.ajax)

Suggestion: Create an interface on the server. The interface directly accesses Baidu's API and then returns the result.

The format of the data returned by the server is incorrect. json should not be returned. This format should be returned:
Callback ({Your json data content });

This type of api does not provide ajax query. You must add a specified apikey in the http request header.

You are not clearJSONPRequest? The returned result isJavaScriptCode insteadJSONData.

The problem is cross-origin.
Php is acceptable because the apikey is successfully set in the request header of the Baidu server,
Javascript is not allowed because cross-origin rules fail. Specifically, the pre-Request options returned from Baidu's http header value Access-Control-Request-Headers: *. The verification fails. This item must list the specific values (such as apikey ), cannot be a wildcard.
Reference https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.