Use JSONP to complete cross-domain requests, but the requested link is JS file, the request is always that JS error, how MO do?

Source: Internet
Author: User
Replacing the link with PHP is no problem.

$.ajax ({type: ' GET ', Async:false,url: "Http://app.veishu.com/jsonp/categories/all.js", DataType: "Jsonp", Success: function (data) {alert ("Success"),//alert (data[0]),//var datahtml= ""//For (var i=0; data.length;i++) {//datahtml+= "";} $ (". List"). html (datahtml);},error:function () {alert ("Ajax is Error");});

This Ajax does not go into error, nor does it enter success.
In that link JS that error.
If you replace the link with PHP, you will successfully enter success.
But this JS file link directly in the browser open also no problem, can appear json. I don't know what's going on.
There are also browser open JS file has Unicode characters, how to turn!

Reply content:

Using jquery for JSONP calls, the JSON data returned requires a callback name, and because your return value is static data, you should specify the name of the Jsonpcallback.

JQuery (document). Ready (function () {         $.ajax ({             type: "Get",             async:false,             URL: "// app.veishu.com/jsonp/categories/all.js?1.2.1 ",             dataType:" Jsonp ",             jsonpcallback:" Flighthandler ",// The custom JSONP callback function name             success:function (data) {             },             error:function () {}});     });

The returned all.js data format is as follows:

Flighthandler ({"Success": TRUE, "data" [{"Name": "\u624b\u5de5diy", "position": 0, "slug": "handicraft"}]});

The principle of JSONP: segmentfault.com/q/1010000002608394/a-1020000002608399
The static resource file callback does not return a JS file in the form of an execution function after the added GET parameter, so the load fails.

App.veishu.com/jsonp/categories/all.js can you put down the All.js code??

If you remember correctly, jsonp the request requests that the data returned by the server json be () wrapped up in parentheses.

Jsonp's principle is to request a section of JS, all.js syntax error.

Your app.veishu.com/jsonp/categories/all.js return data structure is impossible to do Jsonp, JSONP should return a similar data structure callback(data); which is data the JSON data you want to return

"Recommended"

1. Special recommendation : "PHP Programmer Toolkit" V0.1 version download

2. PHP Free Video Tutorial

3. PHP Development Verification Code latest video tutorial

  • 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.